Last Updated:

A complete guide to the Java 8 Date Time API

What does the new Java Date Time API give us?

Before we start to deal with the new Java 8 Date Time API, let's look at the main problems in dealing with the date and time of Java 7 versions and below:

  1. The Java Date Time classes are packaged. So, the class is in both the package and the package. The classes for formatting and parsing are defined in the package.Date Classjava.utilв java.sqljava.text
  2. The package contains both the date and the time, while it contains only the date. In my opinion, both classes are not very well designed. java.util.Datejava.sql.Date
  3. All classes for working with a date can change, so they are not thread-safe. This is one of the biggest problems in the Date and Calendar classes.
  4. The Date class does not provide internationalization, does not support time zones. So classes and , but again, they have all the problems listed above.java.util.Calendarjava.util.TimeZone

In view of all the shortcomings listed above, third-party developers created a library for working with the date and time. It is called Joda Time and is very often used as a replacement for standard Java classes. But that's the topic of another article.

Parsing the Java 8 Date Time API

The Java 8 Date Time API is designed to replace the old classes for working with time and date. Let's take a look at the main packages of the new API.

  1. Package—This is the base package of the new Date Time API. All major base classes are part of this package: , , , , , and others. All of these classes are immutable and thread-safe. In most cases, these classes will be sufficient for most tasks. java.time LocalDateLocalTimeLocalDateTimeInstantPeriodDuration
  2. A package is a package with common interfaces for non-ISO calendar systems. We can inherit a class to create our own calendar system.java.time.chronoAbstractChronology
  3. A package is a package with time and date formatting and parsing classes. In most cases, we won't use them directly because the classes in the package provide convenient methods for formatting and parsing.java.time.formatjava.time
  4. The package is used to work with temporary objects, for example, with its help we can find out the first or last day of the month. The methods of such classes are immediately noticeable against the background of others, because they always have the format ''.java.time.temporalwithXXX
  5. Package - Classes to support different time zones and rules for changing them.java.time.zone

Examples of using the Java Date Time API

We have already covered the most important parts of the Java Date Time API. It's time to deal with the classes and see them work with small examples.

Java.time.LocalDate class

java.time.LocalDate is an immutable class that represents Date objects in the default format. We can use the method to get the current date. We can also provide as arguments the year, month, and day to create an instance of . This class provides an overloaded method that we can provide to get a date in a specific time zone. This class provides the same functionality as . Let's look at a simple example to use it:yyyy-MM-ddnow()LocalDatenow()ZoneIdjava.sql.Date

The result of the above program:

Java.time.LocalTime class

The class is immutable and represents time in a readable form. By default, it provides the . Just like , this class provides support for time zones and date generation by passing hours, minutes, and seconds as arguments. Let's look at the use on an example of a simple program:LocalTimehh:mm:ss.zzzLocalDateLocalTime

The result of the above program:

Example of using java.time.LocalDateTime

Class — represents the date and time in the default format: . To create an instance there is a method that takes and as input arguments. Let's look at its use with a simple example:

java.time.LocalDateTimeyyyy-MM-dd-HH-mm-ss.zzzLocalDateTimeLocalDateLocalTime

Result:

Get current time : 2015-11-21T12:48:00.973
DateTime : 2015-11-21T12:48:00.984
LocalDateTime with specified date : 2017-07-09T11:06:22
2000 seconds after 01/01/1970 : 1970-01-01T00:33:20

Example of using java.time.Instant

The class is used to work with a machine-readable time format - it stores the date and time in the so-called "unix timestamp (timestamp)". An example of use is below:Instant

Result:

Current timestamp : 2015-11-21T11:01:32.610Z
Instant for timestamp : 2015-11-21T11:01:32.610Z
PT1440H

As mentioned earlier, most date and time classes in Java 8 are provided by various helper methods: add or subtract a few days, weeks, months, etc. There are other methods for managing the date using and calculating the difference between two dates. See an example of using helper methods in the Java 8 Date API:TemporalAdjuster

Result:

Example of parsing and date formatting

Let's look at an example of parsing and formatting a date:

Here is the result:

That's all the innovations in the Date Time API Java 8. From all of the above, we can safely conclude that the API was successful: it became much more convenient and understandable. I think that with such a Date Time API, many will refuse to use third-party libraries to work with time and date.