日期时间类、时间戳、间隔类
public class Test1 {
@Test
public void test01() {
LocalDateTime ldt1 = LocalDateTime.now();
System.out.println(ldt1);
LocalDateTime ldt2 = LocalDateTime.of(2020, 1, 23, 8, 30, 10, 10);
System.out.println(ldt2);
LocalDateTime ldt3 = ldt1.plusYears(2);
System.out.println(ldt3);
LocalDateTime ldt4 = ldt3.minusMonths(2);
System.out.println(ldt4.getYear());
System.out.println(ldt4.getMonthValue());
System.out.println(ldt4.getDayOfMonth());
System.out.println(ldt4.getHour());
System.out.println(ldt4.getMinute());
System.out.println(ldt4.getSecond());
}
@Test
public void test02() {
Instant now1 = Instant.now();
System.out.println(now1);
OffsetDateTime odt = now1.atOffset(ZoneOffset.ofHours(8));
System.out.println(odt);
System.out.println(now1.toEpochMilli());
Instant now2 = Instant.ofEpochSecond(1);
System.out.println(now2);
}
@Test
public void test03() throws InterruptedException {
Instant now1 = Instant.now();
Thread.sleep(1000);
Instant now2 = Instant.now();
Duration duration1 = Duration.between(now1, now2);
System.out.println(duration1.toMillis());
System.out.println("-----------------------------");
LocalTime lt1 = LocalTime.now();
Thread.sleep(1000);
LocalTime lt2 = LocalTime.now();
Duration duration2 = Duration.between(lt1, lt2);
System.out.println(duration2.toMillis());
}
@Test
public void test04() throws InterruptedException {
LocalDate ld1 = LocalDate.now();
Thread.sleep(1000);
LocalDate ld2 = LocalDate.of(2021, 12, 31);
Period period = Period.between(ld1, ld2);
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
}
}
日期时间格式化类-DateTimeFormatter
public class Test1 {
@Test
public void test01() {
LocalDateTime ldt1 = LocalDateTime.now();
DateTimeFormatter dtf1 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
String strDateTime1 = ldt1.format(dtf1);
System.out.println(strDateTime1);
DateTimeFormatter dtf2 = DateTimeFormatter.
ofPattern("yyyy年MM月dd日 HH:mm:ss");
String strDateTime2 = ldt1.format(dtf2);
System.out.println(strDateTime2);
LocalDateTime parse = LocalDateTime.parse("2020年03月12日 11:04:14", dtf2);
System.out.println(parse);
}
}
时间矫正器类-TemporalAdjuster
public class Test1 {
@Test
public void test01() {
LocalDateTime ldt1 = LocalDateTime.now();
LocalDateTime ldt2 = ldt1.withMonth(10);
System.out.println(ldt2);
LocalDateTime ldt3 = ldt1.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println(ldt3);
LocalDateTime ldt4 = ldt1.with((temporal)->{
LocalDateTime ldt = (LocalDateTime) temporal;
DayOfWeek week = ldt.getDayOfWeek();
if(week.equals(DayOfWeek.FRIDAY)){
return ldt.plusDays(3);
}else if(week.equals(DayOfWeek.SATURDAY)){
return ldt.plusDays(2);
}else{
return ldt.plusDays(1);
}
});
System.out.println(ldt4);
}
}
时区类
public class Test1 {
@Test
public void test01() {
Set<String> set = ZoneId.getAvailableZoneIds();
for (String str : set) {
System.out.println(str);
}
LocalDateTime ldt1 = LocalDateTime.now(ZoneId.of("Asia/Tokyo"));
System.out.println(ldt1);
LocalDateTime ldt2 = LocalDateTime.now(ZoneId.of("Asia/Tokyo"));
ZonedDateTime zonedDateTime = ldt2.atZone(ZoneId.of("Asia/Tokyo"));
System.out.println(zonedDateTime);
}
}