//
LocalDate格式的:2022-04-25
LocalDate with2 = LocalDateTimeUtil.parseDate("2022-05", "yyyy-MM"
).with(TemporalAdjusters.firstDayOfMonth())
.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
System.out.println(
"with2 = " +
with2);
//
将LocalDateTime格式转为String的yyyy-MM-dd格式 :2022-04-25T00:00 转为 2022-04-25
//
previousOrSame (往前包括当天)最近星期几的日期。如最近星期五的日期,如果今天是星期五,则返回今天日期,如果今天不是星期五,则返回上周五的日期
//
DayOfWeek.MONDAY 星期一
String format = LocalDateTimeUtil.parseDate("2022-05", "yyyy-MM"
).with(TemporalAdjusters.firstDayOfMonth())
.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY))
.format(DateTimeFormatter.ofPattern(
"yyyy-MM-dd"
));
System.out.println(
"format = " +
format);
//
将LocalDateTime格式转为String的yyyy-MM-dd格式 :2022-05-29T23:59:59.999999999 转为 2022-05-29
//
lastInMonth某月的最后一个星期几
//
DayOfWeek.SUNDAY 星期天
String format1 = LocalDateTimeUtil.parseDate("2022-05", "yyyy-MM"
)
.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY))
.format(DateTimeFormatter.ofPattern(
"yyyy-MM-dd"
));
System.out.println(
"format1 = " +
format1);
//
获取当前周的星期天日期 2022-05-01T23:59:59.999999999
//
nextOrSame (往前包括当天)最近星期几的日期。如最近星期五的日期,如果今天是星期五,则返回今天日期,如果今天不是星期五,则返回上周五的日期
LocalDateTime withQuery7 = LocalDate.of(2022, 5, 1
).atTime(LocalTime.MAX).with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
System.out.println(
"with = " +
withQuery7);
//
获取当前周的星期一日期 2022-04-25T00:00
LocalDateTime withQuery1 = LocalDate.of(2022, 5, 1
).atTime(LocalTime.MIN).with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
System.out.println(
"with = " +
withQuery1);
//
获取下一个星期天 2022-05-08T23:59:59.999999999
LocalDateTime localDateTime7 = withQuery7.plusWeeks(1
);
System.out.println(
"localDateTime7 = " +
localDateTime7);
//
获取下一个星期一 2022-05-02T00:00
LocalDateTime localDateTime1 = withQuery1.plusWeeks(1
);
System.out.println(
"localDateTime1 = " +
localDateTime1);
//
获取第五周星期天的月份 6
LocalDateTime localDateTime5Week = localDateTime7.plusWeeks(5
);
System.out.println(
"localDateTime5Week = " +
localDateTime5Week);
int
monthValue =
localDateTime5Week.getMonthValue();
System.out.println(
"monthValue = " +
monthValue);
//
获取当月最后一个星期天 2022-05-29T23:59:59.999999999
LocalDateTime with = LocalDate.of(2022, 5, 1
).atTime(LocalTime.MAX)
.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY));
System.out.println(
"with = " +
with);
//
获取当月第一个星期一 2022-05-02T00:00
LocalDateTime with1 = LocalDate.of(2022, 5, 1
).atTime(LocalTime.MIN)
.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
System.out.println(
"with1 = " +
with1);
三、结果图
四、技术解析
①TemporalAdjusters类中预定义实现
TemporalAdjusters工具类有很多预定义的static方法返回TemporalAdjuster对象,使用不同方式调节Temporal对象而与Temporal实现无关。
包括当前:就是当前时间是星期天,2022.5.1号,就是我们需要的数据,就会返回,如果不包含的话,就会到下一个星期中,就不会是当前周内的数据了
// 获取当前周的星期天日期 2022-05-01T23:59:59.999999999
// nextOrSame (包括当天)
LocalDateTime withQuery7 = LocalDate.of(2022, 5, 1).atTime(LocalTime.MAX).with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
System.out.println("with = " + withQuery7);
// 获取当前周的星期天日期 with = 2022-05-08T23:59:59.999999999
// next (不包括当天)
LocalDateTime withQuery2 = LocalDate.of(2022, 5, 1).atTime(LocalTime.MAX).with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println("with = " + withQuery2);
可以参考资料
https://blog.csdn.net/weixin_49114503/article/details/121676567
* 博客文章部分截图及内容来自于学习的书本及相应培训课程,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。
* 备注:王子威
* 我的网易邮箱:wzw_1314_520@163.com