使用jdk1.8的LocalDate獲取當天,本月第一天,本月最后一天,今年第一天,今年最后一天
LocalDate localDate = LocalDate.now(); //今天 Date day = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); //這個月的第一天 Date monthStart = Date.from(localDate.with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay(ZoneId.systemDefault()).toInstant()); //這個月的最后一天 下個月的第一天 Date monthEnd = Date.from(localDate.plusMonths(1).with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay(ZoneId.systemDefault()).toInstant()); //今年的第一天 Date yearStart = Date.from(localDate.with(TemporalAdjusters.firstDayOfYear()).atStartOfDay(ZoneId.systemDefault()).toInstant()); //今年的最后一天 下一年的第一天 Date yearEnd = Date.from(localDate.plusYears(1).with(TemporalAdjusters.firstDayOfYear()).atStartOfDay(ZoneId.systemDefault()).toInstant());