先引入Hutool-all 工具包
/*
* 获取本周第一天和最后一天
*
* */
public static List<String> getWeekStartAndEndDay() {
// 获取当前日期
LocalDate today = LocalDate.now();
// 获取本周的第一天(周一)
LocalDate firstDayOfWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
// 获取本周的最后一天(周日)
LocalDate lastDayOfWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
ArrayList<String> strings = new ArrayList<>();
strings.add(firstDayOfWeek.toString());
strings.add(lastDayOfWeek.toString());
return strings;
}
/*
* 获取本月第一天和最后一天
*
* */
public static List<String> getMonthStartAndEndDay() {
// 获取当前日期
LocalDate currentDate = LocalDate.now();
// 获取当前月份
YearMonth currentMonth = YearMonth.from(currentDate);
// 获取本月的第一天
LocalDate firstDayOfMonth = currentMonth.atDay(1);
// 获取本月的最后一天
LocalDate lastDayOfMonth = currentMonth.atEndOfMonth();
ArrayList<String> strings = new ArrayList<>();
strings.add(firstDayOfMonth.toString());
strings.add(lastDayOfMonth.toString());
return strings;
}
public static String YMDHMSToYMD(String time){
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(time, dateTimeFormatter);
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String dateString = dateTime.format(dateFormatter);
return dateString;
}