//获取本地日期对象
LocalDate ld = LocalDate.now();
System.out.println(ld);
int year = ld.getYear(); //年
int month = ld.getMonthValue(); //月
int day = ld.getDayOfMonth(); //日
int dayOfYear = ld.getDayOfYear(); //一年中的第几天
int dayOfWeak = ld.getDayOfWeek().getValue(); //星期几
//直接修改某个信息
LocalDate ld2 = ld.withYear(2099);
LocalDate ld3 = ld.withMonth(12);
System.out.println(ld2);
System.out.println(ld3);
//把某个信息加多少
LocalDate ld4 = ld.plusYears(2);
System.out.println(ld4);
//把某个信息减多少
LocalDate ld5 = ld.minusYears(2);
//获取指定日期的LocalDate对象
LocalDate ld6 = LocalDate.of(2048, 12, 12);
LocalDate ld7 = LocalDate.of(2048, 12, 12);
System.out.println(ld6);
//判断2个日期对象,是否相等,在前还是在后
System.out.println(ld6.equals(ld7));
System.out.println(ld6.isAfter(ld));
System.out.println(ld6.isBefore(ld));
//获取本地时间对象
LocalTime lt = LocalTime.now();
System.out.println(lt);
//获取时、分、秒、纳秒
int hour = lt.getHour();
int minute = lt.getMinute();
int second = lt.getSecond();
int nano = lt.getNano();
//直接修改某个信息
LocalTime lt1 = lt.withHour(10);
LocalTime lt2 = lt.withMinute(10);
System.out.println(lt1);
System.out.println(lt2);
//把某个信息加多少
LocalTime lt3 = lt.plusHours(2);
System.out.println(lt3);
//把某个信息减多少
LocalTime lt4 = lt.minusHours(2);
//获取指定时间的LocalTime对象
LocalTime lt5= LocalTime.of(12, 12, 12);
LocalTime lt6 = LocalTime.of(12, 12, 12);
System.out.println(lt5);
//判断2个时间对象,是否相等,在前还是在后
System.out.println(lt5.equals(lt6);
System.out.println(lt5.isAfter(lt));
System.out.println(lt5.isBefore(lt));
//获取本地日期和时间对象
LocalDateTime ldt = LocalDateTime.now();//年 月 日 时 分 秒 纳秒
System.out.println(ldt);
//获取时、分、秒、纳秒
int year = ldt.getYear(); //年
int month = ldt.getMonthValue(); //月
int day = ldt.getDayOfMonth(); //日
int dayOfYear = ldt.getDayOfYear(); //一年中的第几天
int dayOfWeak = ldt.getDayOfWeek().getValue(); //星期几
int hour = ldt.getHour();
int minute = ldt.getMinute();
int second = ldt.getSecond();
int nano = ldt.getNano();
//直接修改某个信息
LocalDateTime ldt1 = ldt.withYear(2099);
LocalDateTime ldt2 = ldt.withMinute(10);
System.out.println(ldt1);
System.out.println(ldt2);
//把某个信息加多少
LocalDateTime ldt3 = ldt.plusHours(2);
LocalDateTime ldt4 = ldt.plusYears(2);
System.out.println(ldt3);
//把某个信息减多少
LocalDateTime ldt5 = ldt.minusHours(2);
//获取指定日期时间的LocalDateTime对象
LocalDateTime ldt6 = LocalDateTime.of(2099,12,12,12,12,12,12);
LocalDateTime ldt7 = LocalDateTime.of(2099,12,12,12,12,12,12);
System.out.println(ldt6);
//判断2个日期时间对象,是否相等,在前还是在后
System.out.println(ldt6.equals(ldt7);
System.out.println(ldt6.isAfter(ldt));
System.out.println(ldt6.isBefore(ldt));
//可以把LocalDateTime转换成LocalDate或LocalTime对象
//也可以把LocalDate和LocalTime合并成LocalDateTime
LocalDate ld = ldt.toLocalDate();
LocalTime lt = ldt.toLocalTime();
LocalDateTime ldt8 = LocalDateTime.of(ld, lt);
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId.getId());
System.out.println(zoneId);
//获取全部时区ID
System.out.println(ZoneId.getAvailableZoneIds());
//把某个时区id封装成ZoneId对象
ZoneId zoneId1 = ZoneId.of("America/Los_Angeles");
//获取某个时区的ZonedDateTime对象
ZonedDateTime now = ZonedDateTime.now(zoneId1);
System.out.println(now);
//世界标准时间
ZonedDateTime now1 = ZonedDateTime.now(Clock.systemUTC());
System.out.println(now1);
//获取系统默认的ZonedDateTime对象
ZonedDateTime now2 = ZonedDateTime.now();
System.out.println(now2);
- Instant的时间线上的某个时刻/时间戳(用来记录代码的执行时间/记录用户操作某个事件的时间点)
//1.创建Instant对象,获取此刻时间信息
Instant now = Instant.now();
//2.获取总秒数
long second = now.getEpochSecond();
System.out.println(second);
//3.不够1秒的纳秒数
int nano = now.getNano();
System.out.println(nano);
System.out.println(now);
Instant instant = now.plusNanos(111);
- DateTimeFormatter(格式化器,用于时间的格式化解析)
//创建一个日期时间格式化器对象
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
//对时间进行格式化
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
String rs = formatter.format(now);//正向格式化
System.out.println(rs);
String rs2 = now.format(formatter);//反向格式化
System.out.println(rs2);
//解析时间:解析时间一般使用LocalDateTime提供的解析方法来解析
String dateStr = "2029年12月12日 12:12:12";
LocalDateTime ldt = LocalDateTime.parse(dateStr, formatter);
System.out.println(ldt);
- Period:计算两个LocalDate对象相差的年数、月数、天数
LocalDate start = LocalDate.of(2029, 8, 10);
LocalDate end = LocalDate.of(2029, 8, 15);
//1.创建Period对象,封装两个日期对象
Period period = Period.between(start, end);
//2.通过Period对象获取两个日期对象相差的信息
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
- Duration:计算两个时间对象相差的天数、小时、分、秒、毫秒、纳秒
LocalDateTime start = LocalDateTime.of(2025, 11, 11, 11, 10, 10, 11);
LocalDateTime end = LocalDateTime.of(2025, 11, 11, 11, 11, 11, 11);
//得到Duration对象
Duration duration = Duration.between(start, end);
//获取两个时间对象间隔的信息
System.out.println(duration.toDays());
System.out.println(duration.toHours());
System.out.println(duration.toMinutes());
System.out.println(duration.toSeconds());
System.out.println(duration.toMillis());