- 日期转换
importcn.hutool.core.date.DateTime; importcn.hutool.core.date.DateUtil; importcn.hutool.core.date.LocalDateTimeUtil; importjava.time.LocalDateTime; publicclassTest { publicstaticvoidmain(String[] args) { StringdateStr="2020-10-10T14:33:56"; DateTimedt=DateUtil.parse(dateStr); // Date对象转换为LocalDateTimeLocalDateTimelocalDateTime=LocalDateTimeUtil.of(dt); // 时间戳转换为LocalDateTimelongtime=dt.getTime(); localDateTime=LocalDateTimeUtil.ofUTC(time); } }
- 日期格式化
importcn.hutool.core.date.DatePattern; importcn.hutool.core.date.LocalDateTimeUtil; importjava.time.LocalDateTime; publicclassTest { publicstaticvoidmain(String[] args) { StringdateStr="2020-10-10T14:33:56"; LocalDateTimelocalDateTime=LocalDateTimeUtil.parse(dateStr); Stringformat=LocalDateTimeUtil.format(localDateTime, DatePattern.NORM_DATETIME_PATTERN); System.out.println(format); } }
- 计算时间间隔
importcn.hutool.core.date.LocalDateTimeUtil; importjava.time.Duration; importjava.time.LocalDateTime; publicclassTest { publicstaticvoidmain(String[] args) { LocalDateTimestart=LocalDateTimeUtil.parse("2022-10-02T00:00:00"); LocalDateTimeend=LocalDateTimeUtil.parse("2022-10-10T00:00:00"); Durationbetween=LocalDateTimeUtil.between(start, end); System.out.println(between.toDays()); } }