In depth analysis of date and time of new features in Java 8_ Power node Java college sorting

Dates are a key part of business logic computing, and any enterprise application needs to deal with time issues. Applications need to know the current point in time and the next point in time, and sometimes they must calculate the path between the two points in time. Make complaints about Java's date before it's disgusting. Let's make complaints about Tucao java.. util. Date and calendar

Tiago Fernandez once voted for the worst Java API and the first EJB 2 10. The second is the date API.

Slot point I

At the beginning, date not only carries date information, but also performs conversion between dates, but also displays different date formats. The responsibilities are complex (don't know a single responsibility, does your mother know? It's a joke ~ ha ha)

Later, starting from JDK 1.1, these three responsibilities were separated:

The corresponding method in the original date is obsolete. However, both date and calendar are inconvenient to use. This is where the API is not well designed.

Slot point II

Pit father's year and month

Output Thu Feb 01 00:00:00 CST 3912

Observe the output result. Year is 2012 + 1900, while month. Didn't I give 1 to the month parameter? How to export February?

Someone should have told you that if you want to set a date, you should use Java util. Calendar, like this

It's wrong to write this way. Calendar month also starts from 0. You should use the number 7 to express August, or simply use enumeration

Note the above code. There is no need to subtract 1900 from the value of calendar year (of course, the definition of month is the same as date). This inconsistency is really crazy!

Some people may know that the API related to calendar is donated by IBM, which leads to inconsistency.

Slot point III

java. util. Date and Java util. All properties in calendar are mutable

The following code calculates the number of days between two dates

There is a problem with daysbetween. If two date instances are calculated continuously, 0 will be obtained the second time, because the calendar status is variable. Considering the situation of repeated calculation, it is best to copy a new calendar

jsr310

The above has led to the birth of some third-party Java date libraries, such as the widely used joda-time and date4j. Although the third-party library is powerful and easy to use, it still has compatibility problems. For example, the standard JSF Date Converter and joda time API are not compatible. You need to write your own converter, so the standard API is still necessary, So there is jsr310.

JSR 310 actually has two date concepts. The first is instant, which roughly corresponds to Java util. The date class, because it represents a definite point in time, that is, the offset from the standard Java era (January 1, 1970); but unlike the java.util.date class, it is accurate to the nanosecond level.

The second corresponds to human concepts, such as localdate and Localtime. They represent the general concept of time zone, Either date (excluding time) or time (excluding date), which is similar to the representation of java.sql. In addition, there is a monthday, which can store someone's birthday (excluding year). Each class stores correct data internally, instead of using midnight to distinguish dates and 1970-01-01 to represent time, as in java.util.date.

At present, Java 8 has implemented all the contents of jsr310. Added Java The classes defined in the time package represent the rules of the date time concept, including instances, durations, dates, times, time zones and periods. These are based on the ISO calendar system, which follows Gregorian rules. The most important point is that the value is immutable and thread safe. Through the following figure, let's take a quick look at Java The format of the values of some main classes under the time package is easy to understand.

Method overview

The API of the package provides a large number of related methods, which generally have consistent method prefixes:

Of: static factory method.

Parse: static factory method, focusing on parsing.

Get: get the value of something.

Is: check whether something is true.

With: immutable setter equivalent.

Plus: add some amount to an object.

Minus: subtract some amount from an object.

To: convert to another type.

At: combine this object with another object, for example: date atTime(time)。

Correspondence with old API

Simple use of Java Time API

Difference from joda time

In fact, Stephen coleborne, the specification leader of jsr310, is also the creator of joda time. Jsr310 is established on the basis of joda time and refers to most APIs, but it does not mean that jsr310 = joda time. The following obvious differences are:

1. The most obvious change is the package name (from org.joda.time and Java. Time)

2. Jsr310 does not accept null value. Joda time regards null value as 0

3. The difference between computer related time (instant) and human related time (datetime) in jsr310 becomes more obvious

4. All exceptions thrown by jsr310 are subclasses of datetimeexception. Although datetimeexception is a runtimeException

summary

Compare old date API

The date and time processing API may be just an insignificant API in various languages. If you don't have complex time processing requirements, you may just use the date and time processing API to obtain the system time and simply display it. However, if you take the date and time seriously, its complexity may be much more than you think, astronomy, geography, history, politics Culture and other factors will affect your handling of time. Therefore, in terms of processing time, it is best to choose jsr310 (if you use java8, you can implement 310), or joda time.

Not only does Java face the embarrassment of time processing, other languages have also encountered similar problems, such as

Arrow: a better date and time processing library in Python

Moment. JS: date library in JavaScript

Noda-Time:. Net camp joda time replication

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>