java正确开发系列:hutool日期处理
背景:
一、把字符串类型的日期转为DateTime
// 假定字符串类型的日期为:2023-01-01 String startDateStr = "2023-01-01"; DateTime startDate = DateUtil.parse(startDateStr); log.Info(startDate); // 输出 2023-01-01 00:00:00
// 输出 2023-01-01T00:00,日期和时间的中间带有T,这种日期格式称为ISO时间,使用hutool要想得到这种格式的,使用下面的代码
startDate.toLocalDateTime();
log.info(startDate.toLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"))); // 输出 2023-01-01T00:00:00
二、根据月初的第一天,计算月末最后一天
// 假设 endDate = "2023-01-01T00:00" LocalDateTime endDate = endDate.toLocalDateTime().plusDays(endDate.getLastDayOfMonth() - 1);