java8日期计算

实际开发中,我们常常对日期进行相关计算操作,Java8日期计算有哪些场景? 

//1.只比较大小的场景
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTime1 = LocalDateTime.
        of(2020,8,5,20,1,0);
System.out.println(localDateTime.compareTo(localDateTime1));// 前面的 -  后面的
//2.计算两个时间具体相差值
Duration duration = Duration.between(localDateTime,localDateTime1);
System.out.println(duration.toMinutes());//后面的 - 前面的
System.out.println(duration.toDays());
System.out.println(duration.toHours());
//3.时间的修改
LocalDate date = LocalDate.of(2017, 1, 5);
LocalDate date1 = date.withYear(2018);
LocalDate date2 = date.withMonth(10);
System.out.println(date1.getYear());
//4.时间的增加,减少
LocalDate date3 = date.plusDays(2);
LocalDate date4 = date.minusDays(3);
System.out.println(date3);
//5.特殊的日期   更多参考枚举   ChronoField
LocalDate date5 = date.with(firstDayOfMonth());
System.out.println(date5);

更多其他场景,欢迎分享

你可能感兴趣的:(JavaSE,java)