packagecom.xq.java8;importorg.junit.jupiter.api.Test;import java.time.*;importjava.time.format.DateTimeFormatter;importjava.time.temporal.TemporalAdjusters;importjava.util.Set;public classTestLocalDateTime {//ZonedDate、ZonedTime、ZonedDateTime
@Testpublic voidtest7(){
Set set =ZoneId.getAvailableZoneIds();
set.forEach(System.out::println);
}
@Testpublic voidtest8(){
LocalDateTime ldt= LocalDateTime.now(ZoneId.of("America/Rosario"));
System.out.println(ldt);
LocalDateTime ldt2= LocalDateTime.now(ZoneId.of("America/Rosario"));
ZonedDateTime zdt= ldt2.atZone(ZoneId.of("America/Rosario"));
System.out.println(zdt);
}//DateTimeFormatter : 格式化时间 / 日期
@Testpublic voidtest6(){
DateTimeFormatter dt=DateTimeFormatter.ISO_DATE;
LocalDateTime ldt=LocalDateTime.now();
String str=ldt.format(dt);
System.out.println(str);
System.out.println("--------------------");
DateTimeFormatter dtf2= DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String strDate2=dtf2.format(ldt);
System.out.println(strDate2);
LocalDateTime newdate=ldt.parse(strDate2,dtf2);
System.out.println(newdate);
}//TemporalAdjuster:时间校正器
@Testpublic voidtest5(){
LocalDateTime ldt=LocalDateTime.now();
System.out.println(ldt);
LocalDateTime ldt2= ldt.withDayOfMonth(10);
System.out.println(ldt2);
LocalDateTime ldt3=ldt.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println(ldt3);//自定义:下一个工作日
LocalDateTime ldt5 = ldt.with((l) ->{
LocalDateTime ldt4=(LocalDateTime)l;
DayOfWeek dow=ldt4.getDayOfWeek();if(dow.equals(DayOfWeek.FRIDAY)){return ldt4.plusDays(3);
}else if(dow.equals(DayOfWeek.SATURDAY)){return ldt4.plusDays(2);
}else{return ldt4.plusDays(1);
}
});
System.out.println(ldt5);
}//1.LocalDate LocalTime LocalDateTime//2. Instant: 时间戳(以 Unix 元年:1970年1月1日 00:00:00 到某个时间的毫秒值)
@Testpublic voidtest2(){
Instant ins1=Instant.now();
System.out.println(ins1);
OffsetDateTime odt= ins1.atOffset(ZoneOffset.ofHours(8));
System.out.println(odt);
System.out.println(ins1.toEpochMilli());
}
@Testpublic voidtest1(){
LocalDateTime ldt=LocalDateTime.now();
System.out.println(ldt);
LocalDateTime ldt2= LocalDateTime.of(2020,5,13,13,11,33);
System.out.println(ldt2);
LocalDateTime ldt3= ldt.plusYears(2);
System.out.println(ldt3);
LocalDateTime ldt4= ldt.minusMonths(2);
System.out.println(ldt4);
System.out.println(ldt.getYear());
System.out.println(ldt.getMonthValue());
System.out.println(ldt.getDayOfMonth());
System.out.println(ldt.getHour());
System.out.println(ldt.getMinute());
}
}