Background: Why the new API

First, explain why you need to create a new API.

1.1 Java.util. Date problems

  • Nondeterministic (fixed) – date instances are not immutable.

  • There are concurrency issues – date instances are not thread-safe.

  • Incorrect naming — Date is not a “Date”, but a “time stamp”.

  • Lack of convention — days start at 1, months at 0, years at 1900.

  • Discontinuity (lack of “fluency”) – cannot create “durations” (e.g., a quarter, 5 minutes) or combinations (e.g., years + months, dates without seconds), etc.

1.2 Date-time apis prior to Java 8

  • System. CurrentTimeInMillis () is not accurate, the API for multiple continuous call returns the same value.

  • Date vs. java.sql.Date – Java.sql. Date is just a Date with no time.

  • Java.sql. Timestamp — Java.sql. Timestamp copy of java.util.Date adds nanosecond storage.

1.3 Problems with java.util.calendar

  • Lack of clarity – mixing dates and times.

  • Chaotic time zone support – not easy to switch time zones, offsets, etc.

  • There are serious formatting barriers – SimpleDateFormat and Calendar don’t interoperate well.

  • This creates an extension difficulty – it is difficult to create a new Calendar system by extending Calendar.

“Calendar is the Date equivalent of the greedy Tral beast, which thinks that if you can’t see it, it can’t see you” — Chandra Guntour, c. 2000.

Two, the solution?

2.1 JSR-310: Date and Time API Extracted from JSR-310 (jcp.org/en/jsr/deta…)

  • The primary goal is to provide a more advanced and comprehensive model for Date and time manipulation, based on the lessons learned from the first two apis in Java SE (Date and Calendar).

  • The new API will target all applications that require a date and time data model. This model will go beyond classes and replace Date and Calendar, including representations of dates without time, times without dates, durations, and intervals.

  • The new API will also address date and time issues. This includes formatting and parsing, taking into account the ISO8601 standard and its implementations, such as XML.

  • The ultimate goal of the new API is ease of use. The API will need to include some powerful features, but don’t let those features overshadow standard use cases. The easy part involves interacting with existing Date and Calendar classes…

2.2 Origin of JSR-310

  • Jsr-310 is “inspired” by the very popular Joda-time library by Stephen Colebourne **_ **, who is also the leader of JSR-310.

  • Jsr-310 is a way to both overcome the disadvantages and refactor the Joda-time part. Blog.joda.org/2009/11/why… .

  • Check from Joda Time conversion to Java. The Time API:blog.joda.org/2014/11/con…

  • Still can look up on stackoverflow Meno Hochschild (Time4J library) of answer: stackoverflow.com/questions/2…

Java8 Date Time class

Dates and Times: Simple “containers” for Dates and Times

The name of the class instructions
Instant Stores a timestamp + nanosecond from the Java era
LocalDate Store dates with no time part (calendar dates)
LocalTime Store time without date part (wall clock)
LocalDateTime Store date and Time (LocalDate + Local Time)
ZonedDateTime Stores dates and times with time zones
OffsetTime Store the time and offset from UTC
OffsetDateTime Stores dates with time and UTC offsets

Ranges and Partials

The name of the class instructions
Duration Time is measured in nanoseconds. (e.g. 5 minutes)
Period Time in years, months and/or days. (e.g., 2 days)
Month Store months. (e.g. MARCH MARCH)
MonthDay Store months and dates without a year or time (for example, date of birth)
Year Store the year. (e.g. 2015)
YearMonth Stores years and months without dates or times. (e.g., expiry date of credit card)
DayOfWeek Store a single day of the week. (for example: WEDNESDAY, WEDNESDAY)

Chronology: a calendar system for organizing and identifying dates

The name of the class instructions
Chronology Yes Factory for creating or getting a pre-built calendar system default isIsoChronology(such as:ThaiBuddhistChronology)
ChronoLocalDate Store dates without time in any chronology
ChronoLocalDateTime Store dates and times in any chronology
ChronoZonedDateTime Stores dates, times, and time zones in any chronology
ChronoPeriod Analog day/time span for any chronology
Era Store timelines [usually two Chronology, but sometimes more]

Java8 Date Time general API chart

The Date and Time:

Ranges:

Partials:

Fifth, streaming and semantic apis

The Java 8 date-time API introduces a certain symmetry into the operation that makes it a pleasant programming experience for developers. The following is a list of prefixes for common methods in the API.

  • Of {class factory method prefix} – Constructs an object with the supplied arguments – validates and constructs do not convert. Example: LocalDate. Of (…). or Instant.ofEpochSecond(…)

  • From {class factory method prefix} – Constructs an object with the supplied parameters – validates, transforms, and builds. Example: LocalDateTime. The from (…). or OffsetTime.from(…)

  • Parse {class factory method prefix} – Obtain the object by parsing the supplied CharSequence argument. Example: LocalDate. Parse (…). or OffsetDateTime.parse(…)

  • Format {object method prefix} – Formats an object with the given time format argument. Example: localDate. The format (the formatter)

  • Get {object method prefix} – Returns the partial state of the target time object. Example: localDate getDayOfWeek ()

  • Is {object method prefix} – Queries the partial state of the target time object. Example: a localTime. IsAfter (…).

  • With {object method prefix} – Changes to parts return a copy of the immutable temporal object. Example: offsetTime. WithHour (…).

  • Plus {object method prefix} – Returns a copy of the time object with the added time. Example: localDate. PlusWeeks (…).

  • Minus {object method prefix} – Returns a copy of the time object minus time. Example: a localTime. MinusSeconds (…).

  • To {object method prefix} – Converts a time object to a new time object of another type. Example: localDateTime. ToLocalDate (…).

  • At {object method prefix} – Combines time objects into a new time object using the arguments provided. Example: localDate. AtTime (…).

Vi. The translator said

Hi, everyone, I am L.CM (Mica author). Thanks to Zhang Yadong (JustAuth, Jap author) and Li Xunhuan (Pig team) for participating in the translation.

This article has been lying in the translation list for more than a year, and it is coming soon that Java17 will be released. However, most students have only a superficial grasp of Java8 time, so I translate it, hoping to help you.

In addition, welcome to pay attention to us, we will regularly collate and translate the industry articles and comb the hot software updates. In addition, our team open source Pig, SpringBlade, Avue, JustAuth, IJPay and Mica and other popular projects, welcome star!!