1、获取当前时间日期
//时间
LocalTime now = LocalTime.now();
System.out.println(now);
//日期
LocalDate ld = LocalDate.now();
System.out.println(ld);
//时间日期
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
2、获取当前时间日期并格式化
//时间
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime lt2 = LocalTime.now();
String now2 = lt2.format(formatter);
System.out.println(now2);
//日期
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate ld2 = LocalDate.now();
String str = ld2.format(dtf);
System.out.println(str);
//时间日期
DateTimeFormatter dtf3 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime ldt2 = LocalDateTime.now();
String str2 = ldt2.format(dtf3);
System.out.println(str2);
3、字符串类型转换为时间日期类型
//时间
LocalTime lt = LocalTime.parse("18:20:55");
System.out.println(lt);
//日期
LocalDate ld3 = LocalDate.parse("2024-11-20");
System.out.println(ld3.getClass());
//时间日期
LocalDateTime ldt3 = LocalDateTime.parse("2024-11-20T20:12:11");
System.out.println(ldt3.getClass());
4、字符串类型转换为LocalTime类型并格式化
//时间
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime lt3 = LocalTime.parse("19:42:55", formatter2);
System.out.println(lt3);
//日期
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate ld4 = LocalDate.parse("2024/11/20",dtf2);
System.out.println(ld4);
//时间日期
DateTimeFormatter dtf4 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime ldt4 = LocalDateTime.parse("2024/11/20 20:13:11",dtf4);
System.out.println(ldt4);
5、获取指定时间
//时间
LocalTime lt4 = LocalTime.of(19,44,50);
System.out.println(lt4);
//日期
LocalDate ld5 = LocalDate.of(2025,11,20);
System.out.println(ld5);
//时间日期
LocalDateTime ldt5 = LocalDateTime.of(2025,11,20,20,14,11);
System.out.println(ldt5);