博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 8 – TemporalAdjusters examples
阅读量:5284 次
发布时间:2019-06-14

本文共 2301 字,大约阅读时间需要 7 分钟。

1. TemporalAdjusters

Example to move a date to firstDayOfMonth, firstDayOfNextMonth, next Monday and etc.

TestDate.java
package com.mkyong.time; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.temporal.TemporalAdjusters; public class TestDate { public static void main(String[] args) { LocalDate localDate = LocalDate.now(); System.out.println("current date : " + localDate); LocalDate with = localDate.with(TemporalAdjusters.firstDayOfMonth()); System.out.println("firstDayOfMonth : " + with); LocalDate with1 = localDate.with(TemporalAdjusters.lastDayOfMonth()); System.out.println("lastDayOfMonth : " + with1); LocalDate with2 = localDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY)); System.out.println("next monday : " + with2); LocalDate with3 = localDate.with(TemporalAdjusters.firstDayOfNextMonth()); System.out.println("firstDayOfNextMonth : " + with3); } }
Copy

Output

current date : 2016-11-15firstDayOfMonth : 2016-11-01lastDayOfMonth : 2016-11-30next monday : 2016-11-21 firstDayOfNextMonth : 2016-12-01
Copy
Note
Please check this   for the entire list of the predefined TemporalAdjusters.
 

2. Custom TemporalAdjuster

Example to implement TemporalAdjuster to move a date to next Christmas.

NextChristmas.java
package com.mkyong.time; import java.time.temporal.ChronoField; import java.time.temporal.Temporal; import java.time.temporal.TemporalAdjuster; public class NextChristmas implements TemporalAdjuster { @Override public Temporal adjustInto(Temporal temporal) { return temporal.with(ChronoField.MONTH_OF_YEAR, 12).with(ChronoField.DAY_OF_MONTH, 25); } }
Copy
TestDate.java
package com.mkyong.time; import java.time.LocalDate; public class TestDate { public static void main(String[] args) { LocalDate localDate = LocalDate.now(); System.out.println("current date : " + localDate); localDate = localDate.with(new NextChristmas()); System.out.println("Next Christmas : " + localDate); } }
Copy

Output

current date : 2016-11-15Next Christmas : 2016-12-25
Copy

Alternatively, you can use the following lambda expression :

localDate = localDate.with( temporal -> temporal.with(ChronoField.MONTH_OF_YEAR, 12).with(ChronoField.DAY_OF_MONTH, 25) ); http://www.mkyong.com/java8/java-8-temporaladjusters-examples/

转载于:https://www.cnblogs.com/shy1766IT/p/10162132.html

你可能感兴趣的文章
ubuntu server设置时区和更新时间
查看>>
【京东咚咚架构演进】-- 好文收藏
查看>>
【HTML】网页中如何让DIV在网页滚动到特定位置时出现
查看>>
文件序列化
查看>>
jQuery之end()和pushStack()
查看>>
Bootstrap--响应式导航条布局
查看>>
Learning Python 009 dict(字典)和 set
查看>>
JavaScript中随着鼠标拖拽而移动的块
查看>>
HDU 1021 一道水题
查看>>
The operation couldn’t be completed. (LaunchServicesError error 0.)
查看>>
php每天一题:strlen()与mb_strlen()的作用分别是什么
查看>>
工作中收集JSCRIPT代码之(下拉框篇)
查看>>
《转载》POI导出excel日期格式
查看>>
code异常处理
查看>>
git - 搭建最简单的git server
查看>>
会话控制
查看>>
推荐一款UI设计软件Balsamiq Mockups
查看>>
Linux crontab 命令格式与详细例子
查看>>
百度地图Api进阶教程-地图鼠标左右键操作实例和鼠标样式6.html
查看>>
游标使用
查看>>