1.LocalDate
@Test
public void localDateTest(){
LocalDate now = LocalDate.now();
System.out.println("现在"+now);
LocalDate localDate= LocalDate.of(2020,2,3);
System.out.println("自定义"+localDate);
System.out.println("年份"+localDate.getYear()+
"月份"+localDate.getMonthValue()+
"本月天"+localDate.getDayOfMonth());
System.out.println("本年天"+localDate.getDayOfYear()+
"获取周几(枚举)"+ localDate.getDayOfWeek());
DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy年MM月dd日");
System.out.println(localDate.format(dateTimeFormatter));
System.out.println("是否在某个日期之前"+localDate.isBefore(now));
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4cd7bcd9a9c96fc4344ea11e648747cb.png)
2.LocalTime
@Test
public void localTimeTest(){
LocalTime now = LocalTime.now();
System.out.println("现在"+now);
LocalTime localTime = LocalTime.of(20, 20, 20);
System.out.println("自定义"+localTime);
System.out.println("小时"+localTime.getHour()+
"分钟"+localTime.getMinute()+
"秒"+localTime.getSecond());
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/61acfe8523d6296983b344402cc5d7bb.png)
3.LocalDateTime
@Test
public void localDateTime(){
LocalDateTime now = LocalDateTime.now();
System.out.println("现在"+now);
LocalDateTime ldt = LocalDateTime.of(2020, 2, 3, 12, 12, 12);
System.out.println(ldt);
System.out.println("设置年"+ldt.withYear(2019)+
"设置月"+ldt.withMonth(12)+
"设置天"+ldt.withDayOfMonth(12));
System.out.println("设置小时"+ldt.withHour(1)+
"设置分钟"+ldt.withMinute(1)+
"设置秒"+ldt.withSecond(1));
System.out.println("下一年"+ldt.plusYears(1)+"上一年"+ldt.minusYears(1));
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bbc258d669b3468d7f71e83e0df54cf7.png)
@Test
public void locaDateTime2(){
LocalDateTime now = LocalDateTime.now();
System.out.println("下周一"+now.with(TemporalAdjusters.next(DayOfWeek.MONDAY)));
System.out.println("本周一"+now.with(l->{
LocalDateTime ldt= (LocalDateTime) l;
int day=ldt.getDayOfWeek().getValue();
return ldt.plusDays(1-day);
}));
System.out.println("本周一"+
now.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).minusWeeks(1));
System.out.println("上周一"+now.with(l->{
LocalDateTime ldt=(LocalDateTime) l;
int value = ldt.getDayOfWeek().getValue();
return ldt.plusDays(1-value).minusWeeks(1);
}));
System.out.println("上周一"+
now.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).minusWeeks(2));
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/317ddc4dd2ec08f2900a2ce2f8d2b682.png)
4.Instant
@Test
public void instantTest(){
Instant ins=Instant.now();
System.out.println(ins.toEpochMilli());
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/90b83151b874e398cbf6bedb5639294e.png)
5.Period Duration
@Test
public void countOfTime(){
Period period = Period.between(LocalDate.of(1990, 1, 1), LocalDate.now());
System.out.println("相差年份"+period.getYears()+
"相差月份"+period.getMonths()+
"相差天数"+period.getDays());
Duration duration = Duration.between(LocalDateTime.of(2020, 5, 14, 1, 1, 1),
LocalDateTime.now());
System.out.println("一共相差多少天"+duration.toDays()+
"相差小时"+duration.toHours()+
"相差秒"+duration.getSeconds());
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a3e2a25a1f674df080d7d1f232fe188f.png)