首頁/ 汽車/ 正文

java時間工具類記錄

本章主要是基於java8中的一些時間類,寫的一些常用的時間轉換、時間計算的方法。比如LocalDateTime,LocalDate,Date等。

在開發的很多時候,經常會遇到一些時間的計算和轉換,像多少天后是星期幾,兩個日期相差天數,時間戳轉為Date日期型別等等,每次遇到都需要翻看以往的專案程式碼查詢。故在此做一下簡單的記錄。

java時間工具類記錄

1.LocalDateTime類

public class LocalDateTimeTool {

public static void main(String[] args) {

System。out。println(getCurrentDateTime());

System。out。println(getLocalDateTimeByString(“2022-05-05 15:35:20。222”, FormatPattern。DATE_TIME_FORMAT。getFormat()));

}

/**

* 獲取當前日期: yyyy-mm-dd hh:mm:ss。SSS

* @return

*/

public static LocalDateTime getCurrentDateTime() {

return LocalDateTime。now();

}

/**

* 字串轉LocalDate

* @return

*/

public static LocalDateTime getLocalDateTimeByString(String dateString, String format){

if (format == null) {

format = FormatPattern。DATE_TIME_FORMAT。getFormat();

}

return LocalDateTime。parse(dateString, DateTimeFormatter。ofPattern(format));

}

/**

* LocalDateTime轉Date: 預設格式

* @param date

* @return

*/

public static Date getDateByLocalDateTime(LocalDateTime date){

return Date。from(date。atZone(ZoneId。systemDefault())。toInstant());

}

/**

* Date 轉 LocalDateTime

* @param date

* @return

*/

public static LocalDateTime getLocalDateTimeByDate(Date date){

return date。toInstant()。atZone(ZoneId。systemDefault())。toLocalDateTime();

}

/**

* 日期時間(LocalDateTime)按指定格式轉字串

*

* @param dateTime

* @param format

* @return

*/

public static String getStringBylocalDateTime(LocalDateTime dateTime, String format) {

String dateString;

if (dateTime == null) {

dateString = “”;

} else {

DateTimeFormatter formatDate = DateTimeFormatter。ofPattern(format);

dateString = dateTime。format(formatDate);

}

return dateString;

}

/**

* Date 相差xxx天的日期

* @param date

* @param value

* @param unit

* @return LocalDateTime

*/

public static LocalDateTime getDifferValueOfDateForLocalDateTime(Date date, Integer value, ChronoUnit unit){

LocalDateTime localDateTime = getLocalDateTimeByDate(date);

return localDateTime。plus(value, unit);

}

/**

* LocalDateTime 轉 LocalDate

* @param localDateTime

* @return

*/

public static LocalDate getLocalDateByLocalDateTime (LocalDateTime localDateTime) {

return localDateTime。toLocalDate();

}

}

public enum FormatPattern {

DATE_TIME_FORMAT(“yyyy-MM-dd HH:mm:ss”),

DATETIME_FORMAT(“yyyyMMddHHmmss”),

DATE_FORMAT(“yyyy-MM-dd”),

DATEFORMAT(“yyyyMMdd”),

private String format;

private FormatPattern(String format) {

this。format = format;

}

public String getFormat() {

return format;

}

}

2.LocalDateTime類

public class LocalDateTool {

public static void main(String[] args) {

System。out。println(getCurrentDate());

System。out。println(getLocalDateByString(“2015-01-01”));

System。out。println(getDateByLocalDate(LocalDate。now()));

System。out。println(getStartOrEndOfWeek(null, true));

System。out。println(getStartOrEndOfMonth(null, false));

System。out。println(getStartOrEndOfQuarter(null, false));

System。out。println(getStartOrEndOfYear(null, false));

System。out。println(getInstantFromLocalDate(getCurrentDate()));

System。out。println(getDifferValueOfDateForLocalDate(new Date(), -5, ChronoUnitDateEnum。MONTHS));

System。out。println(getDifferValueOfDateForLocalDate(new Date(), -5, ChronoUnitDateEnum。YEARS));

System。out。println(getDifferValueOfDateForLocalDate(new Date(), 14, ChronoUnitDateEnum。DAYS));

}

/**

* 獲取當前日期: yyyy-mm-dd

* @return

*/

public static LocalDate getCurrentDate() {

return LocalDate。now();

}

/**

* 字串轉LocalDate

* @return

*/

public static LocalDate getLocalDateByString(String dateString){

return LocalDate。parse(dateString);

}

/**

* LocalDate轉Date: 預設格式

* @param date

* @return

*/

public static Date getDateByLocalDate(LocalDate date){

return Date。from(date。atStartOfDay(ZoneId。systemDefault())。toInstant());

}

/**

* Date 轉 LocalDate

* @param date

* @return

*/

public static LocalDate getLocalDateByDate(Date date){

return date。toInstant()。atZone(ZoneId。systemDefault())。toLocalDate();

}

/**

* Date 相差xxx天的日期

* @param date

* @param value

* @param unit

* @return LocalDate

*/

public static LocalDate getDifferValueOfDateForLocalDate(Date date, Integer value, ChronoUnitDateEnum unit){

LocalDate localDate = getLocalDateByDate(date);

switch (unit。getUnit()) {

case DAYS:

return localDate。plusDays(value);

case WEEKS:

return localDate。plusWeeks(value);

case MONTHS:

return localDate。plusMonths(value);

case YEARS:

return localDate。plusYears(value);

default:

return localDate;

}

}

/**

* 獲取當前周 開始時間和結束時間

* @param date 日期

* @param isFirstDayOfWeek 是否開始

* @return

*/

public static LocalDate getStartOrEndOfWeek(LocalDate date , Boolean isFirstDayOfWeek){

LocalDate now = LocalDate。now();

if (date == null){

date = now;

}

DayOfWeek dayOfWeek = date。getDayOfWeek();

int value = dayOfWeek。getValue();

if (isFirstDayOfWeek){

now = date。minusDays(value - 1);

}else {

now = date。plusDays(7 - value);

}

return now;

}

/**

* 獲取當前周 開始時間和結束時間

* @param date 日期

* @param isFirstDayOfMonth 是否開始

* @return

*/

public static LocalDate getStartOrEndOfMonth(LocalDate date , Boolean isFirstDayOfMonth){

LocalDate now = LocalDate。now();

if (date == null){

date = now;

}

if (isFirstDayOfMonth){

now = date。withDayOfMonth(1);

}else {

now = date。with(TemporalAdjusters。lastDayOfMonth());

}

return now;

}

/**

* 獲取當前周 開始時間和結束時間

* @param date 日期

* @param isFirstDayOfQuarter 是否開始

* @return

*/

public static LocalDate getStartOrEndOfQuarter(LocalDate date , Boolean isFirstDayOfQuarter) {

LocalDate now = LocalDate。now();

if (date == null){

date = now;

}

Month month = date。getMonth();

Month firstMonthOfQuarter = month。firstMonthOfQuarter();

Month endMonthOfQuarter = Month。of(firstMonthOfQuarter。getValue() + 2);

if (isFirstDayOfQuarter){

now = LocalDate。of(date。getYear(), firstMonthOfQuarter, 1);

}else {

now = LocalDate。of(date。getYear(), endMonthOfQuarter, endMonthOfQuarter。length(date。isLeapYear()));

}

return now;

}

/**

* 獲取當前周 開始時間和結束時間

* @param date 日期

* @param isFirstDayOfYear 是否開始

* @return

*/

public static LocalDate getStartOrEndOfYear(LocalDate date , Boolean isFirstDayOfYear){

LocalDate now = LocalDate。now();

if (date == null){

date = now;

}

if (isFirstDayOfYear){

now = LocalDate。of(date。getYear(), Month。JANUARY, 1);

}else {

now = LocalDate。of(date。getYear(), Month。DECEMBER, Month。DECEMBER。length(date。isLeapYear()));

}

return now;

}

/**

* LocalDate 轉換成 Instant時間戳

* @param date

* @return

*/

public static Instant getInstantFromLocalDate(LocalDate date){

return date。atStartOfDay()。toInstant(ZoneOffset。UTC);

}

}

public enum ChronoUnitDateEnum {

DAYS(ChronoUnit。DAYS),

WEEKS(ChronoUnit。WEEKS),

MONTHS(ChronoUnit。MONTHS),

YEARS(ChronoUnit。YEARS),

private ChronoUnit unit;

private ChronoUnitDateEnum(ChronoUnit unitType) {

this。unit = unitType;

}

public ChronoUnit getUnit() {

return unit;

}

}

3.LocalTime類

public class LocalTimeTool {

public static void main(String[] args) {

System。out。println(getCurrentTime());

System。out。println(getLocalTimeByString(“20:15:15。256”));

System。out。println(getDifferValueOfTimeForLocalTime(getCurrentTime(), 50, ChronoUnitTimeEnum。MINUTES));

}

/**

* 獲取當前時間: hh:mm:ss。SSS

* @return

*/

public static LocalTime getCurrentTime() {

return LocalTime。now();

}

/**

* 字串轉LocalTime

* @return

*/

public static LocalTime getLocalTimeByString(String dateString){

return LocalTime。parse(dateString);

}

/**

* LocalTime 相差XXX的時間

* @param time

* @param value

* @param unit

* @return

*/

public static LocalTime getDifferValueOfTimeForLocalTime(LocalTime time, Integer value , ChronoUnitTimeEnum unit){

switch (unit。getUnit()) {

case SECONDS:

return time。plusSeconds(value);

case MINUTES:

return time。plusMinutes(value);

case HOURS:

return time。plusHours(value);

default:

return time;

}

}

}

public enum ChronoUnitTimeEnum {

SECONDS(ChronoUnit。SECONDS),

MINUTES(ChronoUnit。MINUTES),

HOURS(ChronoUnit。HOURS),

private ChronoUnit unit;

private ChronoUnitTimeEnum(ChronoUnit unitType) {

this。unit = unitType;

}

public ChronoUnit getUnit() {

return unit;

}

}

相關文章

頂部