日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

java8新特性 LocalDateTime,LocalDate ,LocalTime 使用,Period 获取时间差

發(fā)布時間:2024/4/24 综合教程 89 生活家
生活随笔 收集整理的這篇文章主要介紹了 java8新特性 LocalDateTime,LocalDate ,LocalTime 使用,Period 获取时间差 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
LocalDateTime使用
DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//獲取當(dāng)前時間
LocalDateTime date=LocalDateTime.now();
System.out.println("YEAR"+date.getYear());
System.out.println("MONTH"+date.getMonth());
System.out.println("DAY"+date.getDayOfMonth());
System.out.println("HOUR"+date.getHour());
System.out.println("MINUTES"+date.getMinute());
System.out.println("SECOND"+date.getSecond());

System.out.println("localDateTime"+dateTimeFormatter.format(date));
//當(dāng)前年的第一天
LocalDateTime firstdayOfYear =date.with(TemporalAdjusters.firstDayOfNextYear());
System.out.println("firstdayOfYear"+dateTimeFormatter.format(firstdayOfYear));
//當(dāng)前年的最后一天
LocalDateTime enddayOfYear =date.with(TemporalAdjusters.lastDayOfYear());
System.out.println("enddayOfYear"+dateTimeFormatter.format(enddayOfYear));
//獲取當(dāng)前月的第一天
LocalDateTime firstdayOfMonth =date.with(TemporalAdjusters.firstDayOfMonth());
System.out.println("firstdayOfMonth"+dateTimeFormatter.format(firstdayOfMonth));
//獲取當(dāng)前月的最后一天
LocalDateTime endDayOfMonth=date.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("endDayOfMonth"+dateTimeFormatter.format(endDayOfMonth));
//獲取下個月第一天
LocalDateTime firstdayOfNextMonth=date.with(TemporalAdjusters.firstDayOfNextMonth());
System.out.println("firstdayOfNextMonth"+dateTimeFormatter.format(firstdayOfNextMonth));
//月增加一個月
LocalDateTime addMonth=date.plusMonths(1);
System.out.println("addMonth"+dateTimeFormatter.format(addMonth));
//月減少一個月
LocalDateTime lessenMonth=date.minusMonths(1);
System.out.println("lessenMonth"+dateTimeFormatter.format(lessenMonth));
//天增加一天
LocalDateTime addOfDay=date.plusDays(1);
System.out.println("addOfDay"+dateTimeFormatter.format(addOfDay));
//天減少一天
LocalDateTime lessOfDay=date.minusDays(1);
System.out.println("lessOfDay"+dateTimeFormatter.format(lessOfDay));

TemporalAdjusters方法
TemporalAdjusters.dayOfWeekInMonth() //同一個月中每一周的第幾天
TemporalAdjusters.firstDayOfMonth() //當(dāng)月的第一天
TemporalAdjusters.firstDayOfNextMonth() //下月的第一天
TemporalAdjusters.firstDayOfYear() //當(dāng)年的第一天
TemporalAdjusters.firstDayOfNextYear() //明年的第一天
TemporalAdjusters.firstInMonth() //同一個月中,第一個符合星期幾要求的值
TemporalAdjusters.lastDayOfMonth() //當(dāng)月的最后一天
TemporalAdjusters.lastDayOfYear() // 當(dāng)年的最后一天
TemporalAdjusters.lastInMonth() //同一個月中,最后一個符合星期幾要求的值
TemporalAdjusters.next() //第一個符合指定星期幾要求的值
TemporalAdjusters.previousOrSame()/nextOrSame()

LocalDate 獲取當(dāng)前時間
LocalDate localDate=LocalDate.now();
System.out.println("localDate"+localDate);
System.out.println("getYear"+localDate.getYear());
System.out.println("getMonth"+localDate.getMonth());
System.out.println("getDayOfMonth"+localDate.getDayOfMonth());

LocalTime 獲取當(dāng)前時間
LocalTime localTime=LocalTime.now();
System.out.println("localTime"+localTime);

Period 獲取時間差
Period period=Period.between(LocalDate.of(2014,2,5),LocalDate.of(2014,2,5));
System.out.println("period"+period);



總結(jié)

以上是生活随笔為你收集整理的java8新特性 LocalDateTime,LocalDate ,LocalTime 使用,Period 获取时间差的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。