New Date and Time API in Java 8
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
Class | Description |
---|---|
Clock | A clock providing access to the current instant, date and time using a time-zone. |
Duration | A time-based amount of time, such as '34.5 seconds'. |
Instant | An instantaneous point on the time-line. |
LocalDate | A date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03. |
LocalDateTime | A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30. |
LocalTime | A time without a time-zone in the ISO-8601 calendar system, such as 10:15:30. |
MonthDay | A month-day in the ISO-8601 calendar system, such as --12-03. |
OffsetDateTime | A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00. |
OffsetTime | A time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 10:15:30+01:00. |
Period | A date-based amount of time in the ISO-8601 calendar system, such as '2 years, 3 months and 4 days'. |
Year | A year in the ISO-8601 calendar system, such as 2007. |
YearMonth | A year-month in the ISO-8601 calendar system, such as 2007-12. |
ZonedDateTime | A date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris. |
ZoneId | A time-zone ID, such as Europe/Paris. |
ZoneOffset | A time-zone offset from Greenwich/UTC, such as +02:00. |
of
– static factory methodparse
– static factory method focussed on parsingget
– gets the value of somethingis
– checks if something is truewith
– the immutable equivalent of a setterplus
– adds an amount to an objectminus
– subtracts an amount from an objectto
– converts this object to another typeat
– combines this object with another, such asdate.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
date1.isAfter(date2)
– It returnstrue
is date1 comes after date2; elsefalse
.date1.isBefore(date2)
– It returnstrue
is date1 comes before date2; elsefalse
.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 LocalDate, LocalTime, and LocalDateTime classes provide a human view of date and time without any reference to time zone.
- The ZoneId, ZoneRules, 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.