java1.8新特性時間與日期


//獲取當前時間
LocalDateTime currentTime = LocalDateTime.now();
System.out.println("當前時間: " + currentTime);
//把當前時間轉成當前日期。比如:2019-10-1
LocalDate date1 = currentTime.toLocalDate();
System.out.println("date1 = " + date1);

//獲取月份
Month month = currentTime.getMonth();
//獲取日期
int day = currentTime.getDayOfMonth();
//獲取秒
int seconds = currentTime.getSecond();
System.out.println("月:"+month+",日:"+day+",秒:"+seconds);

//把當前年換成2012年月換成10月
LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2012);
System.out.println("date2 = " + date2);

//2014-12-12,Month.DECEMBER是12月。第一個參數是年,第二個參數是月,第三個參數是日
LocalDate date3 = LocalDate.of(2014,Month.DECEMBER,12);
System.out.println("date3 = " + date3);

//這是設置時間,小時分。最后輸出是22:15,不包含日期
LocalTime date4 = LocalTime.of(22,15);
System.out.println("date4 = " + date4);

//把字符串轉成時間類型
LocalTime date5 = LocalTime.parse("20:15:30");
System.out.println("date5 = " + date5);

//時區下的示例
//獲取當前時間日期
//Asia : 亞洲 Shanghai :上海
ZonedDateTime date6 = ZonedDateTime.parse("2019-10-02T10:21:32+05:50[Asia/Shanghai]");
System.out.println("date6 = " + date6);

//Europe :歐洲 Paris:巴黎
ZoneId id = ZoneId.of("Europe/Paris");
System.out.println("id = " + id);


//獲取系統默認的時區
ZoneId currentZone = ZoneId.systemDefault();
System.out.println("當前時區 : " + currentZone);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM