Date and Time API in Java prior to Java 8 always scare the ordinary developers due to inadequate support for the date and time use cases.

Before Java 8, the classes (such as java.util.Date and SimpleDateFormatter) aren’t thread-safe, leading to potential concurrency issues for user which made the average developer to deal with serious conversion and formatting issues when writing date-handling code.

Some of the date and time classes also exhibit quite poor API design. For example, years in java.util.Date start at 1900, months start at 1, and days start at 0—not very intuitive.

These issues, and several others, have led to the popularity of third-party date and time libraries, such as Joda-Time.

In order to address these problems and provide better support in the JDK core, a new date and time API in Java 8, which is free of these problems, has been designed for Java SE

The project has been led jointly by the author of Joda-Time (Stephen Colebourne) and Oracle, under JSR 310, and will appear in the new Java SE 8 package java.time.

Let’s see Package java.time

The classes defined here represent the principle date-time concepts, including instants, durations, dates, times, time-zones and periods.
ClassDescription
ClockA clock providing access to the current instant, date and time using a time-zone.
DurationA time-based amount of time, such as '34.5 seconds'.
InstantAn instantaneous point on the time-line.
LocalDateA date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.
LocalDateTimeA date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.
LocalTimeA time without a time-zone in the ISO-8601 calendar system, such as 10:15:30.
MonthDayA month-day in the ISO-8601 calendar system, such as --12-03.
OffsetDateTimeA date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00.
OffsetTimeA time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 10:15:30+01:00.
PeriodA date-based amount of time in the ISO-8601 calendar system, such as '2 years, 3 months and 4 days'.
YearA year in the ISO-8601 calendar system, such as 2007.
YearMonthA year-month in the ISO-8601 calendar system, such as 2007-12.
ZonedDateTimeA date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris.
ZoneIdA time-zone ID, such as Europe/Paris.
ZoneOffsetA time-zone offset from Greenwich/UTC, such as +02:00.
The API has a relatively large surface area in terms of number of methods. This is made manageable through the use of consistent method prefixes.
  • of – static factory method
  • parse – static factory method focussed on parsing
  • get – gets the value of something
  • is – checks if something is true
  • with – the immutable equivalent of a setter
  • plus – adds an amount to an object
  • minus – subtracts an amount from an object
  • to – converts this object to another type
  • at – combines this object with another, such as date.atTime(time)

Examples using LocalDateTime class

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;

public class DateTimeAPI {

  public static void main(String[] args) {
    
    System.out.printf("now: %s%n", LocalDateTime.now());

    System.out.printf("Apr 15, 1994 @ 11:30am: %s%n",
                      LocalDateTime.of(1994, Month.APRIL, 15, 11, 30));

    System.out.printf("now (from Instant): %s%n",
                      LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()));

    System.out.printf("6 months from now: %s%n",
                      LocalDateTime.now().plusMonths(6));

    System.out.printf("6 months ago: %s%n",
                      LocalDateTime.now().minusMonths(6));
  }
}

Output as follows:

now: 2020-04-26T00:48:19.209
Apr 15, 1994 @ 11:30am: 1994-04-15T11:30
now (from Instant): 2020-04-26T00:48:19.217
6 months from now: 2020-10-26T00:48:19.217
6 months ago: 2019-10-26T00:48:19.218

DateTime Comparison in Java 8

Methods

  1. date1.isAfter(date2) – It returns true is date1 comes after date2; else false.
  2. date1.isBefore(date2) – It returns true is date1 comes before date2; else false.
  3. date1.compareTo(date2) – It returns negative value if less, positive value if greater and zero if its equal.
import java.time.LocalDateTime;

public class DateComparison {

  public static void main(String[] args) {
    
    LocalDateTime date1 = LocalDateTime.now();
    
    LocalDateTime date2 = LocalDateTime.now().minusDays(5);
    
    System.out.println(date1.isAfter(date2));
    
    System.out.println(date1.isBefore(date2));
    
    System.out.println(date1.compareTo(date2));
  }
}

Output as below:

true
false
5

As above example, New Date and Time API in Java 8 provides various options to perform related queries more easy and competent with other languages.

Conclusion:

The java.time package contains many classes that your programs can use to represent time and date. This is a very rich API. The key entry points for ISO-based dates are as follows:

  • The Instant class provides a machine view of the timeline.
  • The LocalDateLocalTime, and LocalDateTime classes provide a human view of date and time without any reference to time zone.
  • The ZoneIdZoneRules, and ZoneOffset classes describe time zones, time zone offsets, and time zone rules.
  • The ZonedDateTime class represents date and time with a time zone. The OffsetDateTime and OffsetTime classes represent date and time, or time, respectively. These classes take a time zone offset into account.
  • The Duration class measures an amount of time in seconds and nanoseconds.
  • The Period class measures an amount of time using years, months, and days.