public static void main(String[] args) {
LocalDate nowDate = LocalDate.now();
System.out.println("本年第一天:" + nowDate.with(TemporalAdjusters.firstDayOfYear()));
System.out.println("本年最后一天:" + nowDate.with(TemporalAdjusters.lastDayOfYear()));
// 本季度第一天
int month = nowDate.getMonthValue();
LocalDate firstDayOfQuarter;
if (month <= 3) {
firstDayOfQuarter = LocalDate.of(nowDate.getYear(), Month.JANUARY, 1);
} else if (month <= 6) {
firstDayOfQuarter = LocalDate.of(nowDate.getYear(), Month.APRIL, 1);
} else if (month <= 9) {
firstDayOfQuarter = LocalDate.of(nowDate.getYear(), Month.JULY, 1);
} else {
firstDayOfQuarter = LocalDate.of(nowDate.getYear(), Month.OCTOBER, 1);
}
System.out.println("本季度第一天:" + firstDayOfQuarter);
System.out.println("本季度最后一天:" + nowDate.plusMonths(3 - nowDate.getMonthValue() % 3).with(TemporalAdjusters.lastDayOfMonth()));
System.out.println("本月第一天:" + nowDate.with(TemporalAdjusters.firstDayOfMonth()));
System.out.println("本月最后一天:" + nowDate.with(TemporalAdjusters.lastDayOfMonth()));
// DayOfWeek可指定一周从星期几开始
System.out.println("本周第一天:" + nowDate.with(DayOfWeek.MONDAY));
System.out.println("本周最后一天:" + nowDate.with(DayOfWeek.SUNDAY));
}
结果示例:文章来源:https://www.toymoban.com/news/detail-513368.html
文章来源地址https://www.toymoban.com/news/detail-513368.html
到了这里,关于java 8 获取时间日期 查询本周、本月、本季度、本年的第一天和最后一天的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!