The design concept of Java's Date and Time API is interesting

I haven't used the Date and Time API (JSR 310 []) that came from Java SE 8 so far (since I wasn't particularly dissatisfied with Joda-Time []), but I looked it up the other day. , I found the design concept interesting, so I will write it.

Package overview

Since there are many java.time.chrono [] that contain calendars such as the Japanese calendar, the number of classes looks large.

package Overview interface class Enum exception
java.time Main API 0 15 2 1
java.time.chrono Calendar such as Japanese calendar[^chrono] 6 11 4 0
java.time.format Analysis / output 0 3 4 1
java.time.temporal Adjust the time[^temporal] 7 6 2 1
java.time.zone Time zone 0 4 1 1
total 13 39 13 4

java.time [] At first glance, there are many classes below, but many of them correspond to the words you usually use, so you can swallow them easily.

Immutable

There is only one mutable class, java.time.format.DateTimeFormatterBuilder []. Some things, such as java.time.temporal.TemporalAdjuster [], don't have to be immutable, but they kindly tell you (sour your mouth?) That you should make them immutable.

Implementation requirements: This interface does not limit the implementation to be mutable, but it is highly recommended that it be immutable.

Method naming convention

In addition to the standard get and to, the following words are used as method prefixes. You can also find with, plus, and minus in Joda-Time [].

Avoid inheritance as much as possible

Most classes do not use inheritance (directly inherit from Object), with the exception of exception classes. This is in contrast to Joda-Time Joda-Time.javadoc, which made heavy use of inheritance. Even in the case of using inheritance, the abstract base class inherits Object directly, and the inherited subclass is final to avoid deep inheritance.

Thoroughly eliminate existing classes

Java.nio.Path [] introduced from Java SE 7 can be converted to each other with the existing java.io.File [] and toFile (), toPath (). I will. On the other hand, mutual conversion with the old API is implemented only on the old API side.

I got the feeling that ** you (existing class) are allowed to step on the soil of java.time **. At first, it seems that no mutual conversion was prepared [^ 1], and I even felt a grudge against the old API (ヽ ´ω`)

in conclusion

It's also written in Effective Java, but I sympathize with it because it caused unexpected bugs by not making it immutable, and I was crying because it was inherited without permission because I did not make it final. There are many places to do. I will actively use it in the future.

[^ 1]: Date and Time API Tips for Japanese (ja) --notepad

Recommended Posts

The design concept of Java's Date and Time API is interesting
What is the LocalDateTime class? [Java beginner] -Date and time class-
Check the actual date and time at parse with Java's SimpleDateFormat
Let's consider the meaning of "stream" and "collect" in Java's Stream API.
Explanation of Ruby Time and Date objects
Date and time
Format the date and time given by created_at
Date and time acquisition method using DateAndTime API
The date time of java8 has been updated
[Java] Get and display the date 10 days later using the Time API added from Java 8.
In Time.strptime,% j (total date of the year) is
The comparison of enums is ==, and equals is good [Java]
Speed comparison at the time of generation at the time of date conversion
When the month of the date is acquired, the January shift
Usability of date and time classes in each language
How is the next value of the Time object correct?
Handling of date and time in Ruby. Use Date and Time properly.
It is difficult to use the empty string or date of DBUnit, so fix it and use it.
Handle Java 8 date and time API with Thymeleaf with Spring Boot
When using the constructor of Java's Date class, the date advances by 1900.
Set the date and time from the character string with POI
[Ruby] Creating code using the concept of classes and instances
Now is the time to get started with the Stream API
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
Why the width of the full screen element is 100% and the height is 100vh
Android --Is the order of serial processing and parallel processing of AsyncTask guaranteed? ??
Get the current date and time by specifying the time zone in Thymeleaf
The relationship between strict Java date checking and daylight savings time
[RESTful API] Differences and design of path parameters, query parameters, request body
'% 02d' What is the percentage of% 2?
The Biostar Handbook is too interesting
This and that of the JDK
[MySQL] [java] Receive date and time
About the classification and concept of Immutable / Mutable / Const / Variable of Java and Kotlin.
[Java] How to get the current date and time and specify the display format
I tried to summarize the key points of gRPC design and development
The content of the return value of executeBatch is different between 11g and 12c
This and that of the implementation of date judgment within the period in Java
I translated the grammar of R and Java [Updated from time to time]
Java Unit Test Library-Determine if Artery-Date, Calendar, long is the current date and time (specify tolerance in minutes)