An overview of the

  • LocalDate date
  • The LocalTime time
  • LocalDateTime Date + time
  • ZonedDateTime Date + time + Time zone
  • Instant timestamp
  • OffsetTime
  • OffsetDateTime

The date of such

LocalDate, YearMonth, MonthDay, and Year

LocalDate

// 2021-11-20 LocalDate date = LocalDate.of(2021, Month.NOVEMBER, 20); LocalDate nextWed = date.with(temporaladjusters.next (dayofweek.wednesday)); // LocalDate nextWed = date.with(temporaladjusters.next (dayofweek.wednesday)); / / formatting output System. Out. Println (DateTimeFormatter. OfLocalizedDate (FormatStyle. FULL). The format (date));Copy the code

YearMonth

Yearmonth.lengthofmonth (), returns the number of days in the month

Yearmonth.lengthofyear (), returns the number of days in the year

// 2021-06: 30
YearMonth date = YearMonth.now();
System.out.printf("%s: %d%n", date, date.lengthOfMonth());
// 2021-02: 28
YearMonth date2 = YearMonth.of(2021, Month.FEBRUARY);
System.out.printf("%s: %d%n", date2, date2.lengthOfMonth());
// 2012-02: 366
YearMonth date3 = YearMonth.of(2012, Month.FEBRUARY);
System.out.printf("%s: %d%n", date3, date3.lengthOfYear());
Copy the code

MonthDay

Indicates the day of a month

// FEBRUARY 29 MonthDay date = MonthDay. Of (month.February, 29); Boolean validLeapYear = date.isvalidYear (2010);Copy the code

Year

Boolean validLeapYear = year.of (2012).isleap ();Copy the code

Date and time classes

LocalTime

The LocalTime class does not store time zone or daylight saving time information

LocalTime thisSec = LocalTime.now();
System.out.println(thisSec.getHour() + ":" + thisSec.getMinute() + ":" + thisSec.getSecond());     
Copy the code

LocalDateTime

The value is a combination of LocalDate and LocalTime. No time zone

// now() System.out.printf("now: %s%n", LocalDateTime.now()); // of(), 1994年 15 月 11:30am system.out.printf ("Apr 15, 1994 @ 11:30am: %s%n", LocalDateTime.of(1994, Month.APRIL, 15, 11, 30)); // ofInstant(), based on Instant class, nsec + time zone id system.out.printf ("now (from Instant): %s%n", LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault())); // plusMonth(), 6 months after system.out.printf ("6 months from now: %s%n", localDatetime.now ().plusmonth (); System.out.printf("6 months ago: %s%n", localDatetime.now ().minusmonths (6));Copy the code

Time zone and offset classes

// Offset = +8:00 zonedDatetime.now ()Copy the code

ZoneId + ZoneOffset

ZoneId: Specifies the time zone identifier and provides rules for converting between Instant and LocalDateTime

ZoneOffset: indicates the time ZoneOffset of Greenwich mean time/UTC

/ / get all the available time zone Set < String > allZones = ZoneId. GetAvailableZoneIds (); ZonedDateTime dt = localDatetime.now (); ZoneId zone = ZoneId.of(s); ZonedDateTime zdt = dt.atZone(zone); // time ZoneOffset for IUTC time ZoneOffset = zdt.getoffset ();Copy the code

ZonedDateTime

Dates and times with corresponding time zones are processed using the Greenwich/UTC time zone offset

Combines LocalDateTime with the zoneID class

Represents the full date and time with a time zone

/ / DateTimeFormatter object for formatting ZonedDateTime instance DateTimeFormatter format. = DateTimeFormatter ofPattern (" YYYY - MM - dd HH:mm:ss"); // Time + date LocalDateTime + ZoneId => Time + date + ZonedDateTime // 2013-07-20 19:30:00 LocalDateTime leaving = LocalDateTime.of(2013, Month.JULY, 20, 19, 30); ZoneId leavingZone = ZoneId.of("America/Los_Angeles"); ZonedDateTime departure = ZonedDateTime.of(leaving, leavingZone); // format String departure. Format (format); ZoneId arrivingZone = zoneid. of("Asia/Tokyo"); ZoneId arrivingZone = zoneid. of("Asia/Tokyo"); ZonedDateTime arrival = departure.withZoneSameInstant(arrivingZone); Arrival = arrival. PlusMinutes (650); // Format String out2 = arrival.format(format); Arrivingzone.getrules ().isdaylightsavings (arrival.toinstant ()));Copy the code

OffsetDateTime

Date and time are processed using the corresponding time zone offset of Greenwich/UTC, but the time zone ID is not included

Combines LocalDateTime with the ZoneOffset class

// 2017.07.20 19:30 LocalDateTime localDate = LocalDateTime. Of (2013, month.July, 20, 19, 30); ZoneOffset offset = ZoneOffset.of("-08:00"); OffsetDateTime offsetDate = OffsetDateTime.of(localDate, offset);Copy the code

OffsetTime

The time is processed using the corresponding time zone offset of Greenwich/UTC, but the time zone ID is not included

ZonedDateTime and OffsetDateTime

LocalDateTime date = localDatetime. of(2018, 05, 01, 0, 0, 0); // ZonedDateTime, 2018-05-01t00:00 +08:00[GMT+08:00], ZoneId with a specified ID ZonedDateTime d1 = ZonedDateTime. Of (date, ZoneId.systemDefault()); ZoneOffset = ZoneOffset. Of ("+08:00"); // OffsetDateTime, 2018-05-01t00:00 +08:00, offset does not have ID, because multiple ids may have the same value. OffsetDateTime d2 = OffsetDateTime.of(date, offset);Copy the code

Commonly used API

// 2018-04-30T20:00+04:00[Asia/Yerevan] // Change the time to the specified time zone d1.withZoneSameInstant(ZoneId.of("Asia/Yerevan")); // 2018-05-01t00:00 +04:00[Asia/Yerevan] // d1.withZoneSameLocal(zoneid. of("Asia/Yerevan"));Copy the code

withZoneSameInstant

ToEpochSecond is called to convert the current nanosecond with the specified offset to the new nanosecond

withZoneSameLocal

It doesn’t convert the time, it just changes the time zone

Instant

Instant timestamp = Instant.now(); // add 1h Instant oneHourLater = Instant.now().plushours (1); Long secondsFromEpoch = instant.ofepochsecond (0L). Until (instant.now (), chronounit.seconds);Copy the code

Parsing and formatting

The DateTimeFormatter class provides a number of predefined formatting options that can also be customized

The DateTimeFormatter class is immutable and thread-safe

parse

// Pre-defined String in = "20111203"; LocalDate date = LocalDate.parse(in, DateTimeFormatter.BASIC_ISO_DATE); . / / custom DateTimeFormatter formatter = DateTimeFormatter ofPattern (" MMM d yyyy "); LocalDate date = LocalDate.parse(input, formatter);Copy the code

format

ZoneId leavingZone = ZoneId.systemDefault();
ZonedDateTime departure = ZonedDateTime.of(LocalDateTime.now(), leavingZone);
DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy  hh:mm a");
String out = departure.format(format);
Copy the code

Dft.parse (“2018-05-08 16:03:55”) returns an object that contains no LocalDate but only LocalTime

Parse ("2018-05-08 16:03:55", DTF); parse("2018-05-08 16:03:55", DTF); dtf.parse("2018-05-08 16:03:55").query(LocalDateTime::from);Copy the code

Zq99299.github. IO /java-tutori…