在過去的Java版本中,日期和時間的處理主要依賴于java.util.Date和java.util.Calendar類,然而隨著業(yè)務(wù)系統(tǒng)的復(fù)雜以及技術(shù)層面的提升,這些傳統(tǒng)的日期時間類暴露出了若干顯著的不足之處。隨著Java8的發(fā)布,其引入了一套全新的日期時間API,徹底改變了我們處理日期和時間的方式。
相比較Java8中新引入的java.time包下的時間處理類,傳統(tǒng)的日期時間處理類在易用性,線程安全,不支持市時區(qū)等缺點。
Date currentDate = new Date(); // 輸出原始的日期時間,通常不是人類可讀格式 Fri Mar 08 03:13:47 CST 2024System.out.println(currentDate);// 要改變?nèi)掌诘哪硞€部分,必須先將其轉(zhuǎn)換為 Calendar,然后設(shè)置 Calendar calendar = Calendar.getInstance(); calendar.setTime(currentDate); // 修改日期的天數(shù) calendar.add(Calendar.DAY_OF_MONTH, 1);
Date類內(nèi)部維護了一個 long 類型的瞬時值,當(dāng)調(diào)用如setTime()方法來更新這個瞬時值時,不同的線程同時調(diào)用就會互相覆蓋彼此的值,造成數(shù)據(jù)不一致。
Calendar類不僅包含了日期和時間信息,還有一系列內(nèi)部狀態(tài)變量,如年、月、日、小時、分鐘、秒等。Calendar類的方法通常會修改這些內(nèi)部狀態(tài),例如 add()、set() 等。在多線程環(huán)境下,若多個線程嘗試同時修改同一個 Calendar 實例,也會導(dǎo)致不可預(yù)期的結(jié)果。
SimpleDateFormat類在執(zhí)行格式化和解析日期時間操作時,內(nèi)部會維護一個 Calendar對象以及其他一些狀態(tài)變量。在 format() 或 parse() 方法執(zhí)行過程中,這些狀態(tài)會被更新以完成格式轉(zhuǎn)換。并且SimpleDateFormat中的方法并非原子操作,因此在多線程并發(fā)調(diào)用時,可能在一個線程還未完成整個操作時就被另一個線程打斷,導(dǎo)致錯誤的日期時間處理結(jié)果。
Java8中引入的LocalDate,LocalTime,LocalDateTime這幾個位于java.time下的類克服了上述傳統(tǒng)類別的局限性,提供了更強大、直觀和精準(zhǔn)的日期時間處理能力,成為現(xiàn)代Java開發(fā)中處理日期時間首選的工具類。相比較傳統(tǒng)的日期時間類,具備以下顯著優(yōu)勢:
java.time下主要有如下一些關(guān)鍵類:
這些類共同構(gòu)成了一個強大、靈活且易于使用的日期時間處理體系,大大改善了Java在處理日期時間問題時的效率和準(zhǔn)確性。接下來我們在使用上分別介紹這些類,以及使用他們的方式,感受他們的強大。
LocalTime localTime = LocalTime.now(); System.out.println("localTime:"+localTime); LocalDate localDate = LocalDate.now(); System.out.println("localDate:"+localDate); LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("localDateTime:"+localDateTime);
輸出為:
localTime:15:28:45.241181localDate:2024-03-11localDateTime:2024-03-11T15:28:45.260655
針對LocalTime,LocalDateTime獲取當(dāng)前時刻默認(rèn)會帶有毫秒,如果不需要毫秒的話,可以通過設(shè)置納秒為0 保留秒 1秒 = 十億納秒 。例如:
LocalTime localTime = LocalTime.now().withNano(0);System.out.println("localTime:"+localTime); LocalDateTime localDateTime = LocalDateTime.now().withNano(0); System.out.println("localDateTime:"+localDateTime);
輸出為:
localTime:15:32:31localDateTime:2024-03-11T15:32:31
而對于LocalDateTime獲取當(dāng)前日期,默認(rèn)toString會帶有T分隔日期和時間,在項目中,可以通過全局序列化,進行統(tǒng)一的時間格式輸出為 yyyy-MM-dd HH:mm:ss。但是一般不建議這么干,畢竟改變?nèi)中蛄谢渲茫ㄗh不使用toString,可以使用DateTimeFormatter進行自定義轉(zhuǎn)換。
// of方法直接傳遞對應(yīng)的年、月、日 LocalDate localDate = LocalDate.of(2024, 3, 11); System.out.println("localDate:"+localDate); localDate = LocalDate.of(2024, Month.MARCH, 11); System.out.println("localDate:"+localDate); localDate = LocalDate.ofYearDay(2024, 71); System.out.println("localDate:"+localDate); // 北京時間對應(yīng)的時區(qū) ZoneId chinaTimeZone = ZoneId.of("Asia/Shanghai"); // 創(chuàng)建一個 Instant,這里使用當(dāng)前時間的 InstantInstant instant = Instant.now(); localDate = LocalDate.ofInstant(instant, chinaTimeZone); System.out.println("localDate:"+localDate); // 使用ofEpochDay()方法,則EpochDay為從公元1970年1月1日(Unix紀(jì)元)開始的第多少天 localDate = LocalDate.ofEpochDay(LocalDate.now().toEpochDay()); System.out.println("localDate:"+localDate); LocalTime localTime = LocalTime.of(1, 30); System.out.println("localTime:"+localTime); localTime = LocalTime.of(1, 30, 30); System.out.println("localTime:"+localTime); localTime = LocalTime.ofInstant(instant, chinaTimeZone); System.out.println("localTime:"+localTime); // 根據(jù)一天中的總秒數(shù)構(gòu)建時間localTime = LocalTime.ofSecondOfDay(localTime.toSecondOfDay()); System.out.println("localTime:"+localTime); LocalDateTime localDateTime = LocalDateTime.of(2024, 3, 11, 1, 30, 30); System.out.println("localDateTime:"+localDateTime); localDateTime = LocalDateTime.of(2024, Month.MARCH, 11, 1, 30, 30); System.out.println("localDateTime:"+localDateTime); // 使用LocalDate和LocalTime組合構(gòu)造 localDateTime = LocalDateTime.of(localDate, localTime); System.out.println("localDateTime:"+localDateTime); localDateTime = LocalDateTime.ofInstant(instant, chinaTimeZone); System.out.println("localDateTime:"+localDateTime);
輸出為:
localDate:2024-03-11localDate:2024-03-11localDate:2024-03-11localDate:2024-03-11localDate:2024-03-11localTime:01:30localTime:01:30:30localTime:16:41:37.893310localTime:16:41:37localDateTime:2024-03-11T01:30:30localDateTime:2024-03-11T01:30:30localDateTime:2024-03-11T16:41:37localDateTime:2024-03-11T16:41:37.893310
from()方法將TemporalAccessor類型(如ZonedDateTime)轉(zhuǎn)換為相對應(yīng)的日期或者時間。TemporalAccessor接口是一個用于讀取或?qū)懭肴掌凇r間或者日期時間的通用接口。
// 創(chuàng)建一個ZonedDateTime實例 ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("Asia/Shanghai")); LocalTime localTime = LocalTime.from(zonedDateTime); System.out.println("localTime:"+localTime); LocalDate localDate = LocalDate.from(zonedDateTime); System.out.println("localDate:"+localDate); LocalDateTime localDateTime = LocalDateTime.from(zonedDateTime); System.out.println("localDateTime:"+localDateTime);
輸出為:
localTime:17:18:27.942911localDate:2024-03-11localDateTime:2024-03-11T17:18:27.942911
將字符串按照指定格式(可選)解析為對應(yīng)的日期時間類。
LocalTime localTime = LocalTime.parse("17:25:30"); System.out.println("localTime:"+localTime); localTime = LocalTime.parse("17:25:30", DateTimeFormatter.ofPattern("HH:mm:ss")); System.out.println("localTime:"+localTime); LocalDate localDate = LocalDate.parse("2024-03-11"); System.out.println("localDate:"+localDate); localDate = LocalDate.parse("2024/03/11", DateTimeFormatter.ofPattern("yyyy/MM/dd")); System.out.println("localDate:"+localDate); LocalDateTime localDateTime = LocalDateTime.parse("2024-03-11T17:25:30"); System.out.println("localDateTime:"+localDateTime); localDateTime = LocalDateTime.parse("2024/03/11 17:25:30", DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss")); System.out.println("localDateTime:"+localDateTime);
輸出為:
localTime:17:25:30localTime:17:25:30localDate:2024-03-11localDate:2024-03-11localDateTime:2024-03-11T17:25:30localDateTime:2024-03-11T17:25:30
// LocalTime + LocalDate = LocalDateTimeLocalDateTime localDateTime = LocalTime.now().atDate(LocalDate.now());System.out.println("localDateTime:"+localDateTime);localDateTime = LocalDate.now().atTime(LocalTime.now());System.out.println("localDateTime:"+localDateTime);localDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.now());System.out.println("localDateTime:"+localDateTime);// LocalDateTime 轉(zhuǎn) LocalDateLocalDate localDate = LocalDateTime.now().toLocalDate();System.out.println("localDate:"+localDate);// LocalDateTime 轉(zhuǎn) LocalTimeLocalTime localTime = LocalDateTime.now().toLocalTime();System.out.println("localTime:"+localTime);// 獲取今日開始時間 2024-03-11T00:00localDateTime = LocalDate.now().atStartOfDay();System.out.println("localDateTime:"+localDateTime);// 獲取今日開始時間 2024-03-11T00:00LocalDateTime startDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);System.out.println("startDateTime:"+ startDateTime);// 獲取今日結(jié)束時間 2024-03-11T23:59:59.999999999LocalDateTime endDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);System.out.println("endDateTime:"+ endDateTime);
輸出為:
localDateTime:2024-03-11T18:04:22.348539localDateTime:2024-03-11T18:04:22.370562localDateTime:2024-03-11T18:04:22.370768localDate:2024-03-11localTime:18:04:22.371062localDateTime:2024-03-11T00:00startDateTime:2024-03-11T00:00endDateTime:2024-03-11T23:59:59.999999999
主要使用format 和 parse 進行轉(zhuǎn)換,使用方法基本相同。使用 DateTimeFormatter.ofPattern() 定義時間格式,再進行轉(zhuǎn)換。DateTimeFormatter線程安全。
// LocalTime 轉(zhuǎn) String 自定義輸出格式,例如:**時**分**秒 該轉(zhuǎn)化的 00 不會被省略String localTimeStr = LocalTime.now().format(DateTimeFormatter.ofPattern("HH時mm分ss秒"));System.out.println("localTimeStr:"+localTimeStr);String localDateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));System.out.println("localDateStr:"+localDateStr);// LocalDateTime 轉(zhuǎn) StringString localDateTimeStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));System.out.println("localDateTimeStr:"+localDateTimeStr);// String 轉(zhuǎn) LocalDateTimeLocalDateTime localDateTime = LocalDateTime.parse("2023-04-14 15:59:40", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));System.out.println("localDateTime:"+localDateTime);
輸出結(jié)果:
localTimeStr:19時02分58秒localDateStr:2024-03-11localDateTimeStr:2024-03-11 19:02:58localDateTime:2023-04-14T15:59:40
// Date 轉(zhuǎn) LocalDateTimeDate currentDate = new Date();// 轉(zhuǎn)換為Instant Instant instant = currentDate.toInstant();// 通過zoneId設(shè)置時區(qū)(這里使用系統(tǒng)時區(qū)),轉(zhuǎn)換為帶帶時區(qū)的 ZoneDateTimeZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());// 然后通過ZonedDateTime轉(zhuǎn)換為LocalDateTimeLocalDateTime localDateTime = zonedDateTime.toLocalDateTime();System.out.println("localDateTime:"+localDateTime);// LocalDateTime 轉(zhuǎn) Date,同理也是通過ZonedDateTime轉(zhuǎn)換為DateDate localDateTimeToDate = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());System.out.println(localDateTimeToDate);// Date轉(zhuǎn)LocalDate 同理 LocalDateTime轉(zhuǎn)換LocalDate localDate = currentDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();System.out.println("localDate:"+localDate);// LocalDate 轉(zhuǎn) Date 需要先將 LocalDate 轉(zhuǎn) LocalDateTimeDate localDateToDate = Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant());
這里介紹一下ZoneId。java.time.ZoneId是Java 8中java.time包中用于表示時區(qū)的類。時區(qū)是地球上的地理位置,用于確定在該位置觀察太陽升落以及規(guī)定當(dāng)?shù)鼐用裆詈蜕虡I(yè)活動時間的標(biāo)準(zhǔn)時間。ZoneId使用IANA時區(qū)數(shù)據(jù)庫提供的時區(qū)標(biāo)識符,這個標(biāo)識符是唯一的,這些標(biāo)識符通常是地區(qū)/城市對,例如“Asia/Shanghai”代表中國上海所在的時區(qū),America/New_York代表美國紐約城市。其實例獲取有兩種方式:
時間戳轉(zhuǎn)換。
long timeMillis = System.currentTimeMillis();// 時間戳(Long) 轉(zhuǎn) LocalDateTimeLocalDateTime localDateTime = Instant.ofEpochMilli(timeMillis).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();System.out.println("localDateTime:"+localDateTime);localDateTime = Instant.ofEpochMilli(timeMillis).atZone(ZoneId.systemDefault()).toLocalDateTime();System.out.println("localDateTime:"+localDateTime);// LocalDateTime 轉(zhuǎn) 時間戳(Long) 秒級long localDateTimeToSecond = LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8));System.out.println("localDateTimeToSecond:"+ localDateTimeToSecond);// LocalDateTime 轉(zhuǎn) 時間戳(Long) 毫秒級long localDateTimeToMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();System.out.println("localDateTimeToMilliSecond:"+ localDateTimeToMilliSecond);// 時間戳(Long) 轉(zhuǎn) LocalDateLocalDate localDate = Instant.ofEpochMilli(timeMillis).atZone(ZoneOffset.ofHours(8)).toLocalDate();System.out.println("localDate:"+ localDate);// LocalDate 轉(zhuǎn) 時間戳(Long) 秒級long localDateToSecond = LocalDate.now().atStartOfDay().toEpochSecond(ZoneOffset.ofHours(8));System.out.println("localDateToSecond:"+ localDateToSecond);// LocalDate 轉(zhuǎn) 時間戳(Long) 毫秒級long localDateToMilliSecond = LocalDate.now().atStartOfDay().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();System.out.println("localDateToMilliSecond:"+ localDateToMilliSecond);
輸出結(jié)果為:
localDateTime:2024-03-11T19:37:02.335localDateTime:2024-03-11T19:37:02.335localDateTimeToSecond:1710157022localDateTimeToMilliSecond:1710157022365localDate:2024-03-11localDateToSecond:1710086400localDateToMilliSecond:1710086400000
java.time.ZoneOffset是Java8中java.time包內(nèi)用來表示時區(qū)偏移量的類,它表示的是格林尼治標(biāo)準(zhǔn)時間或協(xié)調(diào)世界時間(UTC)基礎(chǔ)上的固定偏移量。每一個時區(qū)都可以通過一個或多個偏移量來表示,比如“+02:00”表示比UTC時間快兩個小時的時區(qū)偏移。其實例創(chuàng)建有如下方式:
java.time 包中日期時間類(如 LocalDateTime、LocalDate 和 LocalTime)可以通過plusXxx() 和 minusXxx() 方法,用于對日期時間對象進行加減操作,以增加或減少指定的時間或日期單位。
1、LocalDateTime 加減:
// LocalDateTime 加減LocalDateTime localDateTime = LocalDateTime.now();// 以下為增加時、分、秒LocalDateTime plusLocalDateTime = localDateTime.plusHours(1).plusMinutes(1).plusSeconds(1);System.out.println("plusLocalDateTime:"+plusLocalDateTime);plusLocalDateTime = localDateTime.plus(1, ChronoUnit.HOURS).plus(1, ChronoUnit.MINUTES).plus(1, ChronoUnit.SECONDS);System.out.println("plusLocalDateTime:"+plusLocalDateTime);plusLocalDateTime = localDateTime.plus(Duration.ofHours(1)).plus(Duration.of(1, ChronoUnit.MINUTES)).plus(Duration.of(1, ChronoUnit.SECONDS));System.out.println("plusLocalDateTime:"+plusLocalDateTime);// 以下為增加年、月、日plusLocalDateTime = localDateTime.plusYears(1).plusMonths(1).plusWeeks(1).plusDays(1);System.out.println("plusLocalDateTime:"+plusLocalDateTime);plusLocalDateTime = localDateTime.plus(1, ChronoUnit.YEARS).plus(1, ChronoUnit.MONTHS).plus(1, ChronoUnit.WEEKS).plus(1, ChronoUnit.DAYS);System.out.println("plusLocalDateTime:"+plusLocalDateTime);plusLocalDateTime = localDateTime.plus(Duration.of(1, ChronoUnit.YEARS)).plus(Duration.of(1, ChronoUnit.MONTHS)).plus(Duration.of(1, ChronoUnit.WEEKS)).plus(Duration.ofDays(1));System.out.println("plusLocalDateTime:"+plusLocalDateTime);plusLocalDateTime = localDateTime.plus(Period.ofYears(1)).plus(Period.ofMonths(1)).plus(Period.ofWeeks(1)).plus(Period.ofDays(1));System.out.println("plusLocalDateTime:"+plusLocalDateTime);// 以下為減少時、分、秒LocalDateTime minusLocalDateTime = localDateTime.minusHours(1).minusMinutes(1).minusSeconds(1);System.out.println("minusLocalDateTime:"+minusLocalDateTime);minusLocalDateTime = localDateTime.minus(1, ChronoUnit.HOURS).minus(1, ChronoUnit.MINUTES).minus(1, ChronoUnit.SECONDS);System.out.println("minusLocalDateTime:"+minusLocalDateTime);minusLocalDateTime = localDateTime.minus(Duration.ofHours(1)).minus(Duration.of(1, ChronoUnit.MINUTES)).minus(Duration.of(1, ChronoUnit.SECONDS));System.out.println("minusLocalDateTime:"+minusLocalDateTime);// 以下為減少年、月、日minusLocalDateTime = localDateTime.minusYears(1).minusMonths(1).minusWeeks(1).minusDays(1);System.out.println("minusLocalDateTime:"+minusLocalDateTime);minusLocalDateTime = localDateTime.minus(1, ChronoUnit.YEARS).minus(1, ChronoUnit.MONTHS).minus(1, ChronoUnit.WEEKS).minus(1, ChronoUnit.DAYS);System.out.println("minusLocalDateTime:"+minusLocalDateTime);minusLocalDateTime = localDateTime.minus(Duration.of(1, ChronoUnit.YEARS)).minus(Duration.of(1, ChronoUnit.MONTHS)).minus(Duration.of(1, ChronoUnit.WEEKS)).minus(Duration.ofDays(1));System.out.println("minusLocalDateTime:"+minusLocalDateTime);minusLocalDateTime = localDateTime.minus(Period.ofYears(1)).minus(Period.ofMonths(1)).minus(Period.ofWeeks(1)).minus(Period.ofDays(1));System.out.println("plusLocalDateTime:"+minusLocalDateTime);
2、LocalDate 加減:
// LocalDate 加減LocalDate localDate = LocalDate.now();LocalDate plusLocalDate = localDate.plusYears(1).plusMonths(1).plusWeeks(1).plusDays(1);System.out.println("plusLocalDate:"+plusLocalDate);plusLocalDate = localDate.plus(1, ChronoUnit.YEARS).plus(1, ChronoUnit.MONTHS).plus(1, ChronoUnit.WEEKS).plus(1, ChronoUnit.DAYS);System.out.println("plusLocalDate:"+plusLocalDate);plusLocalDate = localDate.plus(Duration.of(1, ChronoUnit.YEARS)).plus(Duration.of(1, ChronoUnit.MONTHS)).plus(Duration.of(1, ChronoUnit.WEEKS)).plus(Duration.ofDays(1));System.out.println("plusLocalDate:"+plusLocalDate);plusLocalDate = localDate.plus(Period.ofYears(1)).plus(Period.ofMonths(1)).plus(Period.ofWeeks(1)).plus(Period.ofDays(1));System.out.println("plusLocalDate:"+plusLocalDate);LocalDate minusLocalDate = localDate.minusYears(1).minusMonths(1).minusWeeks(1).minusDays(1);System.out.println("minusLocalDate:"+minusLocalDate);minusLocalDate = localDate.minus(1, ChronoUnit.YEARS).minus(1, ChronoUnit.MONTHS).minus(1, ChronoUnit.WEEKS).minus(1, ChronoUnit.DAYS);System.out.println("minusLocalDate:"+minusLocalDate);minusLocalDate = localDate.minus(Duration.of(1, ChronoUnit.YEARS)).minus(Duration.of(1, ChronoUnit.MONTHS)).minus(Duration.of(1, ChronoUnit.WEEKS)).minus(Duration.ofDays(1));System.out.println("minusLocalDate:"+minusLocalDate);minusLocalDate = localDate.minus(Period.ofYears(1)).minus(Period.ofMonths(1)).minus(Period.ofWeeks(1)).minus(Period.ofDays(1));System.out.println("minusLocalDate:"+minusLocalDate);
3、LocalTime 加減:
// LocalTime 加減LocalTime localTime = LocalTime.now();LocalTime plusLocalTime = localTime.plusHours(1).plusMinutes(1).plusSeconds(1);System.out.println("plusLocalTime:"+plusLocalTime);plusLocalTime = localTime.plus(1, ChronoUnit.HOURS).plus(1, ChronoUnit.MINUTES).plus(1, ChronoUnit.SECONDS);System.out.println("plusLocalTime:"+plusLocalTime);plusLocalTime = localTime.plus(Duration.ofHours(1)).plus(Duration.of(1, ChronoUnit.MINUTES)).plus(Duration.of(1, ChronoUnit.SECONDS));System.out.println("plusLocalTime:"+plusLocalTime);LocalTime minusLocalTime = localTime.minusHours(1).minusMinutes(1).minusSeconds(1);System.out.println("minusLocalTime:"+minusLocalTime);minusLocalTime = localTime.minus(1, ChronoUnit.HOURS).minus(1, ChronoUnit.MINUTES).minus(1, ChronoUnit.SECONDS);System.out.println("minusLocalDateTime:"+minusLocalTime);minusLocalTime = localTime.minus(Duration.ofHours(1)).minus(Duration.of(1, ChronoUnit.MINUTES)).minus(Duration.of(1, ChronoUnit.SECONDS));System.out.println("minusLocalDateTime:"+minusLocalTime);
LocalDate、LocalTime、LocalDateTime、ZonedDateTime可以通過相對應(yīng)的withXxx()方法修改指定的值。
1、LocalDate:
LocalDate localDate = LocalDate.of(2024, 3, 12);LocalDate newDate = localDate.withYear(2025).withMonth(4).with(ChronoField.DAY_OF_MONTH, 13);System.out.println("newDate:"+newDate);
2、LocalTime:
LocalTime localTime = LocalTime.of(17, 25, 30);LocalTime newTime = localTime.withHour(18).withMinute(26).with(ChronoField.SECOND_OF_MINUTE, 31);System.out.println("newTime:"+newTime);
3、LocalDateTime:
LocalDateTime localDateTime = LocalDateTime.of(2024, 3, 12, 17, 25, 30);LocalDateTime newDateTime = localDateTime.withYear(2025).withMonth(4).with(ChronoField.DAY_OF_MONTH, 13).withHour(18).withMinute(26).with(ChronoField.SECOND_OF_MINUTE, 31);System.out.println("newDateTime:"+ newDateTime);
4、ZonedDateTime:
ZonedDateTime zonedDateTime = ZonedDateTime.of(2024, 3, 12, 17, 25, 30, 0, ZoneId.of("Europe/London"));ZonedDateTime newZonedDateTime = zonedDateTime.withZoneSameInstant(ZoneId.of("America/New_York"));System.out.println("newZonedDateTime:"+ newZonedDateTime);
除此之外,調(diào)整日期時間還可以通過TemporalAdjusters,TemporalAdjuster 是一個函數(shù)式接口,用于根據(jù)給定的規(guī)則調(diào)整日期時間對象。Java8的 java.time.temporal 包中預(yù)定義了一系列常用的 TemporalAdjuster 實現(xiàn),例如獲取下一個工作日、月初、月末等。
LocalDate date = LocalDate.of(2024, 3, 11);// 下一個工作日LocalDate nextWorkingDay = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY)); // 如果11號不是周一,則返回下一個周一的日期// 下一個月的第一天LocalDate firstDayNextMonth = date.with(TemporalAdjusters.firstDayOfMonth()); // 返回4月1日// 當(dāng)月的最后一個工作日LocalDate lastWorkingDay = date.with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY)); // 返回3月最后一個周五的日期// 自定義 TemporalAdjusterTemporalAdjuster adjuster = temporal -> { return temporal.plusDays(10).with(TemporalAdjusters.lastDayOfMonth());};LocalDate tenthDayNextMonthEnd = date.with(adjuster); // 返回4月最后一個日期,前提是先加10天
在Java8及其以后版本的日期時間API中,isBefore() 和 isAfter() 方法是 java.time 包中的 LocalDate、LocalTime、LocalDateTime、ZonedDateTime 等日期時間類所共有的方法,用于比較兩個日期時間對象的先后順序。
isBefore():
LocalDate date1 = LocalDate.of(2024, 3, 11);LocalDate date2 = LocalDate.of(2024, 3, 12);boolean isEarlier = date1.isBefore(date2); // 返回 true,因為 date1 在 date2 之前
isAfter():
LocalDateTime time1 = LocalDateTime.of(2024, 3, 11, 10, 0);LocalDateTime time2 = LocalDateTime.of(2024, 3, 11, 9, 0);boolean isLater = time1.isAfter(time2); // 返回 true,因為 time1 在 time2 之后
compareTo()在Java 8的 java.time 包中,大部分日期時間類如 LocalDate、LocalTime、LocalDateTime、ZonedDateTime 都實現(xiàn)了 Comparable 接口,從而可以直接使用 compareTo() 方法進行比較。compareTo() 方法用于比較兩個日期時間對象的先后順序,返回值含義如下:
LocalDate date1 = LocalDate.of(2024, 3, 11);LocalDate date2 = LocalDate.of(2024, 3, 12);int comparisonResult = date1.compareTo(date2);if (comparisonResult < 0) { System.out.println("date1 is before date2");} else if (comparisonResult > 0) { System.out.println("date1 is after date2");} else { System.out.println("date1 is equal to date2");}LocalDateTime dateTime1 = LocalDateTime.of(2024, 3, 11, 10, 30);LocalDateTime dateTime2 = LocalDateTime.of(2024, 3, 11, 11, 00);int timeComparisonResult = dateTime1.compareTo(dateTime2);
在Java8的 java.time 包中,各個日期時間類如 LocalDate、LocalTime、LocalDateTime 提供了一系列 get 方法,用于獲取特定字段的值。
獲取日期中的特定字段:
LocalDate date = LocalDate.of(2024, 3, 11);int dayOfMonth = date.getDayOfMonth(); // 獲取當(dāng)月的第幾天,此處返回11int monthValue = date.getMonthValue(); // 獲取月份值,此處返回3Month month = date.getMonth(); // 獲取Month枚舉,此處返回Marchint year = date.getYear(); // 獲取年份,此處返回2024
對于時間部分,類似地可以獲取小時、分鐘、秒和納秒:
LocalTime time = LocalTime.of(19, 30, 45);int hour = time.getHour(); // 獲取小時數(shù),此處返回10int minute = time.getMinute(); // 獲取分鐘數(shù),此處返回30int second = time.getSecond(); // 獲取秒數(shù),此處返回45int nano = time.getNano(); // 獲取納秒數(shù)
SpringBoot默認(rèn)集成了Jackson作為JSON處理庫,Jackson已經(jīng)能自動處理 LocalDate、LocalTime 和 LocalDateTime 類型。
如果需要使用自定義日期時間格式,我們有兩種方式:
手動更改全局配置: 如果需要自定義日期格式,可以通過 ObjectMapper 的配置類來注冊自定義的日期格式化器:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId></dependency>
@Configurationpublic class JacksonConfig { @Bean public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() { return builder -> { builder.simpleDateFormat("yyyy-MM-dd"); // 使用Java 8時間API的日期格式器 builder.dateFormat(new StdDateFormat().withColonInTimeZone(true)); // 注冊LocalDateTime的序列化和反序列化模塊 builder.modules(new JavaTimeModule()); }; }}
手動綁定格式化配置SpringBoot支持自動綁定HTTP請求參數(shù)到控制器方法參數(shù)中,包括 LocalDate、LocalTime 和 LocalDateTime 類型。客戶端需發(fā)送符合日期格式的字符串,Spring Boot會自動轉(zhuǎn)換成相應(yīng)類型。
@PostMapping("/events") public ResponseEntity<Event> createEvent(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date, @RequestParam @DateTimeFormat(pattern = "HH:mm:ss") LocalTime startTime, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime timestamp) { // ... }
或者請求或者響應(yīng)VO中:
public static class ResponseVO{ @DateTimeFormat(pattern = "yyyy-MM-dd") private LocalDate date; @DateTimeFormat(pattern = "HH:mm:ss") private LocalTime startTime; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime timestamp; }
在MyBatis中查詢MySQL數(shù)據(jù)庫時,使用Java 8的 java.time.LocalDate、java.time.LocalTime 和 java.time.LocalDateTime類型。
LocalDate 對應(yīng)MySQL的DATE類型。LocalTime 對應(yīng)MySQL 的 TIME 類型。LocalDateTime 對應(yīng)MySQL的 DATETIME或TIMESTAMP類型。
CREATE TABLE `test_date`( `id` BIGINT ( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自增主鍵', test_local_date DATE , test_local_time TIME, test_local_date_time DATETIME, PRIMARY KEY ( `id` ))ENGINE = INNODB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8mb4 COMMENT = '日期時間測試';
@Datapublic class TestDate implements Serializable { /** * 自增主鍵 */ private Long id; private LocalDate testLocalDate; private LocalTime testLocalTime; private LocalDateTime testLocalDateTime; private static final long serialVersionUID = 1L;}
3.MyBatis配置:
@Testpublic void testInsertDate(){ TestDate testDate = new TestDate(); testDate.setTestLocalDate(LocalDate.of(2024, 3, 12)); testDate.setTestLocalTime(LocalTime.of(20,10,30)); testDate.setTestLocalDateTime(LocalDateTime.of(2024, 3, 12,20,10,30,0)); testDateMapper.insert(testDate);}@Testpublic void testQueryDate(){ TestDate testDate = testDateMapper.selectByPrimaryKey(1L); System.out.println("testLocalDate:"+testDate.getTestLocalDate()); System.out.println("testLocalTime:"+testDate.getTestLocalTime()); System.out.println("testLocalDateTime:"+testDate.getTestLocalDateTime());}
image.png
本文探討了Java 8引入的全新日期時間API相較于傳統(tǒng)的Date和Calendar類的優(yōu)勢及實際應(yīng)用。鑒于Java 8新日期時間API在設(shè)計上的先進性和易用性,強烈建議積極采納并替換掉陳舊的Date和Calendar類,轉(zhuǎn)而采用如LocalDate、LocalDateTime、ZonedDateTime等現(xiàn)代日期時間類。
Java 8新日期時間API提供了更為清晰、直觀的操作接口,支持不可變對象設(shè)計模式,增強了類型安全性,并具備豐富的日期時間運算、解析與格式化功能,顯著提高了代碼質(zhì)量與可讀性。此外,新API對日期時間單位的精確度控制、時區(qū)管理以及與其他日期時間規(guī)范的兼容性等方面均表現(xiàn)出卓越的表現(xiàn)力和靈活性,使得我們在處理各類復(fù)雜日期時間邏輯時能夠更加得心應(yīng)手,提升開發(fā)效率。
本文鏈接:http://www.www897cc.com/showinfo-26-76496-0.html還在用Calendar操作Date?Java8都被放棄了,你還不知道Java8中全新的日期時間API
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com