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: 轉(zhuǎn)換到另一個類型。
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轉(zhuǎn)換為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內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java實戰(zhàn)玩具商城的前臺與后臺實現(xiàn)流程
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+JSP+SSM+Springboot+Jsp+maven+Mysql實現(xiàn)一個玩具商城系統(tǒng),大家可以在過程中查缺補漏,提升水平2022-01-01
SpringCloud zookeeper作為注冊中心使用介紹
ZooKeeper由雅虎研究院開發(fā),是Google Chubby的開源實現(xiàn),后來托管到Apache,于2010年11月正式成為Apache的頂級項目。ZooKeeper是一個經(jīng)典的分布式數(shù)據(jù)一致性解決方案,致力于為分布式應用提供一個高性能、高可用,且具有嚴格順序訪問控制能力的分布式協(xié)調(diào)服務2022-11-11
springboot用戶數(shù)據(jù)修改的詳細實現(xiàn)
用戶管理功能作為所有的系統(tǒng)是必不可少的一部分,下面這篇文章主要給大家介紹了關于springboot用戶數(shù)據(jù)修改的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-04-04
java:java.lang.ExceptionInInitializerError報錯解決過程
這篇文章主要給大家介紹了關于java:java.lang.ExceptionInInitializerError報錯的解決過程,java.lang.ExceptionInInitializerError 是一個異常,表示在初始化一個類的靜態(tài)變量或靜態(tài)塊時發(fā)生了錯誤,需要的朋友可以參考下2023-10-10
java中實現(xiàn)Comparable接口實現(xiàn)自定義排序的示例
下面小編就為大家?guī)硪黄猨ava中實現(xiàn)Comparable接口實現(xiàn)自定義排序的示例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09

