Java8時間api之LocalDate/LocalDateTime的用法詳解
引言
在項目中,時間的使用必不可少,而java 8之前的時間api Date和Calander等在使用上存在著很多問題,于是,jdk1.8引進了新的時間api -->LocalDateTime,很好解決了使用時間api所帶來的問題。
先前槽點
1.使用Date或者Calendar表示時間很不方便
Date date = new Date(2022,1,1); System.out.println(date);
會輸出 Thu Feb 01 00:00:00 CST 3922
月份直接變成了Feb,而年份更是加上了1900
如果用calendar 指定月份,則需要注意月份從0開始,也就是表示1月要用0,或者使用提供的枚舉
Calendar calendar = Calendar.getInstance(); calendar.set(2022, Calendar.AUGUST, 2);
但此時,Calendar年份的傳值為2022,與年份一致,這樣就導致了這兩個時間api存在著不一致問題
2.Calendar狀態(tài)可變,在計算時需要創(chuàng)建一個新的Calendar
如calendar在add等操作之后,變成了一個新的Calendar,在下次比較時就可能會出現(xiàn)差錯
calendar.add(Calendar.DAY_OF_MONTH, 1);
3. SimpleDateTimeFormat是非線程安全的。
于是,發(fā)布了新的時間api--LocalDateTime很好解決了上面存在的問題
簡介
LocalDate、 LocalTime、 LocalDateTime類的實例是不可變的對象, 分別表示使用 ISO-8601日歷系統(tǒng)的日期、時間、日期和時間。 它們提供了簡單的日期或時間,并不包含當前的時間信息。也不包含與時區(qū)相關的信息。 顧名思義,LocalDate主要應用在日期,LocalTime主要應用在時間,而LocalDateTime則是時間和日期相結合。
方法概覽
of: 靜態(tài)工廠方法。
parse: 靜態(tài)工廠方法,關注于解析。
get: 獲取某些東西的值。
is: 檢查某些東西的是否是true。
with: 不可變的setter等價物。
plus: 加一些量到某個對象。
minus: 從某個對象減去一些量。
to: 轉換到另一個類型。
at: 把這個對象與另一個對象組合起來,例如: date.atTime(time)。
使用
1.獲取LocalDateTime等對象
通過靜態(tài)方法now() 獲取
LocalDate now = LocalDate.now(); //獲取當前的年月日 2023-05-07 System.out.println(now); LocalTime now1 = LocalTime.now();//獲取當前時間 13:05:46.609 System.out.println(now1); LocalDateTime now2 = LocalDateTime.now(); //獲取當前日期加時間 2023-05-07T13:05:46.609 System.out.println(now2);
通過of()指定年月日時分秒
LocalDate now = LocalDate.now(); //獲取當前的年月日 2023-05-07 System.out.println(now); LocalTime now1 = LocalTime.now();//獲取當前時間 13:05:46.609 System.out.println(now1); LocalDateTime now2 = LocalDateTime.now(); //獲取當前日期加時間 2023-05-07T13:05:46.609 System.out.println(now2);
2.獲取對象相關參數(shù),如年,月
LocalDateTime now = LocalDateTime.now(); int year = now.getYear(); int dayOfYear = now.getDayOfYear(); int hour = now.getHour(); int minute = now.getMinute(); int second = now.getSecond();
3.格式化日期方法 format()以及DateTimeFormatter的使用
LocalDate now = LocalDate.now(); //JDK1.8 格式化日期的類 DateTimeFormatter //指定格式 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); String dateStr = now.format(formatter); System.out.println(dateStr); // 2023年05月07日 LocalDateTime localDateTime = LocalDateTime.now(); DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH點mm分ss秒"); //把日期對象,格式化成日期字符串 String str = localDateTime.format(formatter2); System.out.println(str); //2023年05月07日 13點57分53秒
4. 將LocalDateTime轉換為LocalDate以及LocalTime
System.out.println(localDateTime.toLocalDate()); //2023-05-07 System.out.println(localDateTime.toLocalTime());// 13:57:53.061
5. 比較日期或時間
// isAfter() 判斷一個日期是否在指定日期之后 // isBefore() 判斷一個日期是否在指定日期之前 // isEqual(); // 判斷兩個日期是否相同 // isLeapYear() 判斷是否是閏年注意是LocalDate類中的方法 LocalDateTime now = LocalDateTime.now(); LocalDateTime of = LocalDateTime.of(2023, 5, 7,14,0,0); System.out.println("當前時間為"+now); //當前時間為2023-05-07T14:04:18.676 boolean after = now.isAfter(of); System.out.println(after); // true boolean before = of.isBefore(now); System.out.println(before); //true boolean equal = now.isEqual(of); System.out.println(equal); //false boolean equal1 = of.isEqual(of); System.out.println(equal1); //true //判斷是不是閏年 boolean leapYear = now.toLocalDate().isLeapYear(); System.out.println(leapYear); //false
月份的比較會特殊一點
// now.getMonth().compareTo() 月份之間比較 相同則為0 Month month = LocalDate.now().getMonth(); Month month1 = LocalDate.of(2023, 6, 1).getMonth(); System.out.println(month.compareTo(month1));
6. 將一個日期字符串解析成日期對象
String dateStr = "2022-12-03"; LocalDate parse = LocalDate.parse(dateStr); System.out.println(parse); String dateStr2 = "2022年12月03日"; //按照指定格式來解析 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); LocalDate parse2 = LocalDate.parse(dateStr2, formatter); // System.out.println(parse2); System.out.println("======================"); String dateStr3 = "2021年10月05日 14點20分30秒"; DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH點mm分ss秒"); LocalDateTime parse1 = LocalDateTime.parse(dateStr3, formatter2); System.out.println(parse1);
7. 添加時間 plus
LocalDateTime ldt =LocalDateTime.now(); LocalDateTime localDateTime = ldt.plusYears(1); LocalDateTime localDateTime1 = ldt.plusMonths(3); LocalDateTime localDateTime2=ldt.plusHours(10); LocalDateTime localDateTime2 = ldt.minusYears(8);
8. 計算時間間隔
- Duration : 用于計算兩個“時間”間隔的類
- Period : 用于計算兩個“日期”間隔的類
//Duration:用于計算兩個"時間”間隔的類 Duration betweenNowAnd = Duration.between(preious, end); //long seconds = between.getSeconds(); //間隔的秒值 long l = betweenNowAnd.toMillis(); System.out.println("耗時:" + l + "毫秒"); LocalDate birthday = LocalDate.of(2000, 3, 20); LocalDate nowday = LocalDate.now(); //Period:用于計算兩個“日期”間隔的類 Period between = Period.between(birthday, nowday); int years = between.getYears(); int months = between.getMonths(); int days = between.getDays(); System.out.println(years); System.out.println(months); System.out.println(days);
9.設定時區(qū)
//創(chuàng)建日期對象 LocalDateTime now = LocalDateTime.now(); //獲取不同國家的日期時間根據(jù)各個地區(qū)的時區(qū)ID名創(chuàng)建對象 ZoneId timeID = ZoneId.of("Asia/Shanghai"); //根據(jù)時區(qū)ID獲取帶有時區(qū)的日期時間對象 ZonedDateTime time = now.atZone(timeID); System.out.println(time); //方式2 通過時區(qū)ID 獲取日期對象 LocalDateTime now2 = LocalDateTime.now(ZoneId.of("Asia/Shanghai")); System.out.println(now2);
小結
LocalDate/LocalDateTime都是不可變且線程安全的,位于java.time包中,可以為時區(qū)提供更好的支持,加上DateTimeFormatter之后日期的解析及格式化也更加得心應手。
到此這篇關于Java8時間api之LocalDate/LocalDateTime的用法詳解的文章就介紹到這了,更多相關Java8 LocalDateTime內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決springboot的aop切面不起作用問題(失效的排查)
這篇文章主要介紹了解決springboot的aop切面不起作用問題(失效的排查),具有很好的參考價值,希望對大家有所幫助。 一起跟隨小編過來看看吧2020-04-04springboot集成sensitive-word實現(xiàn)敏感詞過濾的兩種方案
敏感詞過濾通常是指從文本中檢測并移除或替換掉被認為是不適當、冒犯性或違反特定社區(qū)準則的詞匯,這篇文章主要介紹了springboot集成sensitive-word實現(xiàn)敏感詞過濾,需要的朋友可以參考下2024-08-08kafka?消息隊列中點對點與發(fā)布訂閱的區(qū)別說明
這篇文章主要介紹了kafka?消息隊列中點對點與發(fā)布訂閱的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05