获取前一天的起始日期时间和结束日期时间:
如当前时间是2024-01-22,则返回2024-01-21 00:00:00,2024-01-21 11:59:59
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 计算前一天的起始日期时间
LocalDateTime startOfPreviousDay = now.minusDays(1).with(LocalTime.MIN);
String formattedStartOfPreviousDay = startOfPreviousDay.format(formatter);
System.out.println("前一天的起始日期和时间:" + formattedStartOfPreviousDay);
// 计算前一天的结束日期时间
LocalDateTime endOfPreviousDay = now.minusDays(1).with(LocalTime.MAX);
String formattedEndOfPreviousDay = endOfPreviousDay.format(formatter);
System.out.println("前一天的结束日期和时间:" + formattedEndOfPreviousDay);
转化当前String类型的日期时间到指定格式的String类型的日期(20240122):
如有当前String=“2024-01-22 17:30:15” => String=“20240122”
public String toLocalDateString(String localDateTimeString) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateYesterDay = LocalDateTime.parse(localDateTimeString, formatter);
String queryDayValue = localDateYesterDay.toLocalDate().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
}