The traditional time API had thread-safe issues and had to be locked in multithreaded development, so Java8 now provides a new set of time and date APIs.

Here is a diagram of the relationship between time APIs:

1. LocalDate、 LocalTime、 LocalDateTime

Instances of the LocalDate, LocalTime, and LocalDateTime classes are immutable objects that represent the date, time, date, and time, respectively, using the ISO-8601 calendar system. They provide a simple date or time and do not contain the current time information. It also does not contain time zone specific information.

# localDate is the current date, # localTime is the current time, and # localDateTime is a combination of the previous two, using the same. LocalDateTime ldt = LocalDateTime.now(); System.out.println(ldt); LocalDateTime ld2 = LocalDateTime.of(2020, 3, 21, 10, 10, 10); System.out.println(ld2); LocalDateTime ldt3 = ld2.PlusYears (20); System.out.println(ldt3); LocalDateTime ldt4 = ld2.minusmonths (2); System.out.println(ldt4); LocalDateTime now = LocalDateTime. OF (2020, 3, 21, 10, 10, 10); Int year = now.getYear(); // getMonth = Now.getMonth (); // Returns the monthValue int MonthValue = Now.getMonthValue (); Int dayOfMonth = now.getDayOfMonth(); Int dayOfYear = now.getDayOfYear(); DayOfWeek dayOfWeek = now.getDayOfWeek(); // getHour int hour = now.gethour (); Int minute = now.getminute (); Int second = now.getSecond(); Int nano = Now.getNano ();

2. Instant: timestamp. (The milliseconds experienced on January 1, 1970 00:00:00 using UNIX)

Instant ins = Instant.now(); // Default uses UTC timezone system.out.println (ins); OffsetDateTime odt = ins.atOffset(ZoneOffset.ofHours(8)); // Add the offset timezone system.out.println (odt); System.out.println(ins.getNano()); System.out.println(ins.toEpochMilli()); // get timestamp InstantIns2 = Instant.ofepochSecond (5); System.out.println(ins2);

3. Duration and Period

**Duration: to count two “time” intervals; to count two “date” intervals **

Instant ins1 = Instant.now(); System.out.println("--------------------"); try { Thread.sleep(1000); } catch (InterruptedException e) { } Instant ins2 = Instant.now(); System.out.println(" Duration: "+ Duration. BETWEEN (ins1, ins2)); System.out.println("----------------------------------"); LocalDate ld1 = LocalDate.now(); LocalDate ld2 = LocalDate.of(2020, 3, 26); Period pe = Period.between(ld2, ld1); System.out.println(pe.getYears()); System.out.println(pe.getMonths()); System.out.println(pe.getDays()); }

To calculate the total number of days between two dates, you can use the following:

LocalDate dateBefore = LocalDate. Of,1,20 (2015); LocalDate dateAfter = LocalDate. Of,3,25 (2020); long daysBetween = DAYS.between(dateBefore, dateAfter); System.out.println(daysBetween);

EmporalAdjuster: That’s right

LocalDateTime ldt = LocalDateTime.now(); System.out.println(ldt); // reset the current date to 10 localDateTime ldt2 = ldt.withDayOfMonth(10); System.out.println(ldt2); // Get next Sunday LocalDateTime ldt3 = ldt.with(temporalAdjusters. Next (dayofweek.Sunday)); System.out.println(ldt3); With ((t) -> {LocalDateTime ldt4 = (LocalDateTime) t; 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);

5. DateTimeFormatter: Parses and formats the date or time

// dateTimeFormatter DTF = dateTimeFormatter.ISO_LOCAL_DATE; / / you can customize style DateTimeFormatter DTF = DateTimeFormatter. OfPattern (" on dd MM yyyy years HH: MM: ss E "); LocalDateTime ldt = LocalDateTime.now(); String strDate = ldt.format(dtf); System.out.println(strDate); // String back to date. LocalDateTime newLDT = ldt.parse(strDate, DTF); System.out.println(newLdt);

6. ZonedDate, ZonedTime, ZonedDateTime: Time or date with time zone

/ / get all the time zone Set < String > ids = ZoneId. GetAvailableZoneIds (); ids.forEach(System.out::println); LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Asia/Shanghai")); System.out.println(ldt); ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("US/Pacific")); System.out.println(zdt);

7. Conversion from traditional dates