欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > java >内容正文

java

Java8获取年、月、周数据和某一天的开始结束时间

发布时间:2023/12/14 java 50 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Java8获取年、月、周数据和某一天的开始结束时间 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

在Java8中使用 LocalDate 、LocalDateTime、TemporalAdjusters、TemporalField 获取 上周、上个月、去年、本周、本月、今年、下周、下个月、明年的相关数据

public static void main(String[] args) {DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");LocalDate today = LocalDate.now(); // 今天的日期LocalDate todayOfLastWeek = today.minusWeeks(1); // 上周的今天LocalDate todayOfLastMonth = today.minusMonths(1); // 上个月的今天LocalDate todayOfLastYear = today.minusYears(1); // 去年的今天LocalDate todayOfNextWeek = today.plusWeeks(1); // 下周的今天LocalDate todayOfNextMonth = today.plusMonths(1); // 下个月的今天LocalDate todayOfNextYear = today.plusYears(1); // 明年的今天System.out.println("今年的第一天:" + today.with(TemporalAdjusters.firstDayOfYear()).toString());System.out.println("今年的最后一天:" + today.with(TemporalAdjusters.lastDayOfYear()).toString());System.out.println("本月的第一天:" + today.with(TemporalAdjusters.firstDayOfMonth()).toString());System.out.println("本月的最后一天:" + today.with(TemporalAdjusters.lastDayOfMonth()).toString());System.out.println("本周第一天:"+getOneDayOfWeek(today, 1));System.out.println("本周最后一天:"+getOneDayOfWeek(today, 7));System.out.println("***********************************");System.out.println("去年的第一天:" + todayOfLastYear.with(TemporalAdjusters.firstDayOfYear()).toString());System.out.println("去年的最后一天:" + todayOfLastYear.with(TemporalAdjusters.lastDayOfYear()).toString());System.out.println("上个月的第一天:" + todayOfLastMonth.with(TemporalAdjusters.firstDayOfMonth()).toString());System.out.println("上个月的最后一天:" + todayOfLastMonth.with(TemporalAdjusters.lastDayOfMonth()).toString());System.out.println("上周第一天:"+getOneDayOfWeek(todayOfLastWeek,1));System.out.println("上周最后一天:"+getOneDayOfWeek(todayOfLastWeek,7));System.out.println("***********************************");System.out.println("明年的第一天:" + todayOfNextYear.with(TemporalAdjusters.firstDayOfYear()).toString());System.out.println("明年的最后一天:" + todayOfNextYear.with(TemporalAdjusters.lastDayOfYear()).toString());System.out.println("下个月的第一天:" + todayOfNextMonth.with(TemporalAdjusters.firstDayOfMonth()).toString());System.out.println("下个月的最后一天:" + todayOfNextMonth.with(TemporalAdjusters.lastDayOfMonth()).toString());System.out.println("下周第一天:"+ getOneDayOfWeek(todayOfNextWeek,1));System.out.println("下周最后一天:"+ getOneDayOfWeek(todayOfNextWeek,7));System.out.println("***********************************");System.out.println("某天的开始时间:" + dtf.format(LocalDateTime.of(today, LocalTime.MIN)));System.out.println("某天的结束时间:" + dtf.format(LocalDateTime.of(today, LocalTime.MAX)));}/*** 获取一周中的某一天日期* @param today 这周内任意一天的日期* @param day 想要获取一周中的第几天* @return LocalDate*/private static LocalDate getOneDayOfWeek(TemporalAccessor today, int day){TemporalField fieldIso = WeekFields.of(DayOfWeek.MONDAY, 1).dayOfWeek();LocalDate localDate = LocalDate.from(today);return localDate.with(fieldIso, day);}

打印结果:

今年的第一天:2021-01-01 今年的最后一天:2021-12-31 本月的第一天:2021-01-01 本月的最后一天:2021-01-31 本周第一天:2021-01-04 本周最后一天:2021-01-10 *********************************** 去年的第一天:2020-01-01 去年的最后一天:2020-12-31 上个月的第一天:2020-12-01 上个月的最后一天:2020-12-31 上周第一天:2020-12-28 上周最后一天:2021-01-03 *********************************** 明年的第一天:2022-01-01 明年的最后一天:2022-12-31 下个月的第一天:2021-02-01 下个月的最后一天:2021-02-28 下周第一天:2021-01-11 下周最后一天:2021-01-17 *********************************** 某天的开始时间:2021-01-08 00:00:00 某天的结束时间:2021-01-08 23:59:59

技 术 无 他, 唯 有 熟 尔。
知 其 然, 也 知 其 所 以 然。
踏 实 一 些, 不 要 着 急, 你 想 要 的 岁 月 都 会 给 你。


总结

以上是生活随笔为你收集整理的Java8获取年、月、周数据和某一天的开始结束时间的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。