1. LocalDate
LocalDate now = LocalDate.now(); System.out.println(now);//2023-04-07 System.out.println(now.getYear());//2023 System.out.println(now.getMonthValue());//4 System.out.println(now.getDayOfMonth());//7 System.out.println(now.getDayOfWeek());//FRIDAY System.out.println(now.getDayOfWeek().getValue());//5
2. LocalTime
LocalTime now = LocalTime.now(); System.out.println(now);//08:43:06.518 System.out.println(now.getHour());//8 System.out.println(now.getMinute());//43 System.out.println(now.getSecond());//37
3. LocalDateTime
3.1创建 LocalDateTime
// 获取当前系统时间(2022-11-30T16:26:13.158) LocalDateTime now = LocalDateTime.now(); // 指定年月日和时分秒初始化(2022-11-30T10:00:15) LocalDateTime localDateTime = LocalDateTime.of(2022, 11, 30, 10, 00, 15);
3.2 LocalDateTime获取方法
- 获取年
System.out.println(localDateTime.getYear()); // 2022
- 获取月
System.out.println(localDateTime.getMonthValue()); // 11
- 获取日
System.out.println(localDateTime.getDayOfYear()); // 334 System.out.println(localDateTime.getDayOfMonth()); //30 System.out.println(localDateTime.getDayOfWeek()); // WEDNESDAY
- 获取小时和分钟
System.out.println(now.getHour()); //16 System.out.println(now.getMinute()); //34
4. LocalDateTime转换方法
- 转换成为一个LocalDate对象
LocalDate localDate = now.toLocalDate(); // 2022-11-30
- 转换成为一个LocalTime对象
LocalTime localTime = now.toLocalTime(); // 16:36:15.903
LocalDateTime now = LocalDateTime.now(); System.out.println(now);//2023-04-07T08:44:59.799 System.out.println(now.toLocalDate());2023-04-07 System.out.println(now.toLocalTime());//08:44:59.799
4.1 LocalDateTime增加或者减少时间的方法
LocalDate**、LocalTime、LocalDateTime、**Instant 为不可变对象,修改这些对象对象会返回一个副本
LocalDateTime localDateTime = LocalDateTime.of(2022, 11, 30, 13, 14, 52); //2022-11-30T13:14:52 System.out.println(localDateTime); //2023-11-30T13:14:52 System.out.println(localDateTime.plusYears(1)); //2022-11-30T13:15:52 System.out.println(localDateTime.plusMinutes(1)); //2022-12-01T13:14:52 System.out.println(localDateTime.plusDays(1)); //2022-11-30T14:14:52 System.out.println(localDateTime.plusHours(1)); //2022-11-30T13:15:52 System.out.println(localDateTime.plusMinutes(1)); //2022-11-30T13:14:53 System.out.println(localDateTime.plusSeconds(1)); //2022-12-07T13:14:52 System.out.println(localDateTime.plusWeeks(1));
4.2 LocalDateTime修改方法
5. Period
Period:用于计算两个“日期”间隔 。
Period 定义了日期间隔,通过 Period.between 得到了两个 LocalDate 的差,返回的是两个日期差几年零几月零几天。如果希望得知两个日期之间差几天,直接调用 Period 的 getDays() 方法得到的只是最后的“零几天”,而不是算总的间隔天。
LocalDate now = LocalDate.now(); System.out.println(now);//2023-04-07 System.out.println(Period.between(now, LocalDate.of(2021, 12, 24)));//P-1Y-3M-14D System.out.println(Period.between(now, LocalDate.of(2021, 12, 24)).getDays());//-14 System.out.println(Period.between(now, LocalDate.of(2024, 12, 24)));//P1Y8M17D System.out.println(Period.between(now, LocalDate.of(2025, 12, 24)).getDays());//17
API
6. Duration
Duration:用于计算两个“时间”间隔。
7. 格式转换
7.1 时间日期转换为字符串
LocalDate localDate = LocalDate.of(2020, 10, 10); String s1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE); //20201010 String s2 = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE); //2020-10-10
DateTimeFormatter默认提供了多种格式化方式,如果默认提供的不能满足要求,可以通过DateTimeFormatter的ofPattern方法创建自定义格式化方式
LocalDateTime now = LocalDateTime.now(); System.out.println(now);//2023-04-07T09:23:27.606 System.out.println(now.format(DateTimeFormatter.BASIC_ISO_DATE));//20230407 System.out.println(now.format(DateTimeFormatter.ISO_LOCAL_DATE));//2023-04-07 System.out.println(now.format(DateTimeFormatter.ISO_DATE_TIME));//2023-04-07T09:23:27.606 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); System.out.println(now.format(dateTimeFormatter));//2023-04-07 09:24:30
7.2 字符串转换为时间日期
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime localDateTime = LocalDateTime.parse("2023-04-07 10:10:10", formatter); System.out.println(localDateTime);//2023-04-07T10:10:10 DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate localDate = LocalDate.parse("2023-04-09", formatter2); System.out.println(localDate);//2023-04-09
8. LocalDateTime在SpringBoot中的应用
8.1 将LocalDateTime字段以时间戳的方式返回给前端 添加日期转化类
public class LocalDateTimeConverter extends JsonSerializer<LocalDateTime> { @Override public void serialize( LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeNumber(value.toInstant(ZoneOffset.of("+8")).toEpochMilli()); } }
并在 LocalDateTime 字段上添加 @JsonSerialize(using = LocalDateTimeConverter.class) 注解,如下:
@JsonSerialize(using = LocalDateTimeConverter.class) protected LocalDateTime gmtModified;
8.2 将LocalDateTime字段以指定格式化日期的方式返回给前端
在LocalDateTime 字段上添加@JsonFormat(shape=JsonFormat.Shape.STRING, pattern=“yyyy-MM-dd HH:mm:ss”) 注解即可,如下:
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss") protected LocalDateTime gmtModified;
8.3 对前端传入的日期进行格式化
在LocalDateTime字段上添加@DateTimeFormat(pattern = “yyyy-MM-dd HH:mm:ss”) 注解即可,如下:
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") protected LocalDateTime gmtModified;
8.4 前后端日期时间转化问题
在实体类上加@DatetimeFormat与@JsonFormat注解
@DatetimeFormat 将前台日期字符串转换成Date格式 @DateTimeFormat(pattern="yyyy-MM-dd")
@JsonFormat 将服务器端Date日期转换成指定字符串格式 @JsonFormat(pattern="yyyy-MM-dd",timezone="GMT+8")
两个需要同时加,否则会有时区的问题
其他
获取两个时间之间的间隔(天-时-分)
/** * 创建时间: 2023/4/23 * 函数名称: * 函数功能: 获取两个时间之间的 范围 * 函数参数: * * @param begin: 开始 * * @param end: 结束 * @return: String * @author: Snow ******************************************************* * 修改记录(时间--修改人--修改说明): */ public static String getTwoDateTime(LocalDateTime begin, LocalDateTime end){ end = end == null ? LocalDateTime.now() : end; String durationTime; Duration duration = Duration.between(begin, end); // 秒 long seconds = duration.getSeconds(); // 天 int days = (int)(seconds / DAY_SECONDS); // 总秒数 - 天数后剩余的秒数 int remainderSeconds = (int)(seconds % DAY_SECONDS); // 小时0 int hours = remainderSeconds / 3600; // 剩余秒数 remainderSeconds = remainderSeconds % 3600; int minutes = remainderSeconds / 60; durationTime = (days > 0 ? days + "天" : "") + (hours > 0 ? hours + "时" : "") + (minutes > 0 ? minutes + "分钟" : "1分钟"); return durationTime; }