import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
// 定义日期格式
private static final DateTimeFormatter FMT = DateTimeFormatter.ofPattern("yyyyMMdd");
// 自定义格式的系统当前时间
String currentTime = FMT.format(LocalDateTime.now());
String currentTime = LocalDateTime.now().format(FMT);
int currentHour = LocalDateTime.now().getHour();
// 系统当前时间的前一天和后一天
String beforeTime = FMT.format(LocalDateTime.now().minusDays(1))
String afterTime = FMT.format(LocalDateTime.now().plusDays(1)
// 年月日时分秒对应的minus或plus前缀的获取时间API,都是相同格式,返回值均为LocalDateTime类型
minusYears()、
plusMinutes()...
// 获取当天零点
LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN)
// 转换为毫秒时间戳
Long localTimestamp = localDateTime.toEpochSecond(ZoneOffset.of("+8")) * 1000;
// 获取当前时间的整点
LocalDateTime localDateTime = LocalDateTime.of(localDateTime.getYear(), localDateTime.getMonthValue(), localDateTime.getDayOfMonth(), localDateTime.getHour(), 0, 0);
public String transferFormat(String date) {
DateTimeFormatter oldFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime tempDate = LocalDateTime.parse(date, oldFormat);
DateTimeFormatter newFormat = DateTimeFormatter.ofPattern("yyyyMMdd");
String newDate = tempDate.format(newFormat);
return newDate;
}