Whats New in Java 8 - Date API Part II
Whats New in Java 8 - Date API Part II
Join the DZone community and get the full member experience.Join For Free
Secure your Java app or API service quickly and easily with Okta's user authentication and authorization libraries. Developer accounts are free forever. Try Okta Instead.
This post is continues our review of the Date API that came with the release of Java 8. We are going to continue our concentration on classes that make working with dates/times very easy. Working with date objects in previous releases of Java was very challenging with respect to adding time or getting the difference between dates. Hopefully after looking at the classes we present here, your opinion of working with dates and times in Java will change. Specifically, we are going to take a look at the following classes:
- Other classes to represent dates/times
- Getting the current snapshot in time with
- Using the
Clockclass to get system time but specify different time zones
- Represent arbitrary number of days with the
- Represent arbitrary amount of hours with the
Zoned/Offset Dates and Times
In the last post we covered the
LocalDate and the
LocalTime classes. As the name suggests, these classes give the date and or time for a given locality with no time-zone or offset from UTC/Greenwhich time. Java 8 provides the ZonedDateTime class which provides date-times with a time-zone representation. Creating a
ZonedDateTime instance can be done several ways, but here we will demonstrate using two of the many static factory methods:
Next we have the
OffsetDateTime and the
OffsetTime classes that represent date-time or time (respectively) with offsets from UTC/Greenwhich time. Here we show creating
OffsetTime using some of the other available static factory methods:
In the last example we see the use of the
Instant class which we will introduce soon. It’s worth noting at this point that the
OffsetDateTime classes store time to nanosecond precision. Typically we would use the
ZonedDateTime class when showing date-times to users and the
OffsetDateTime class when interacting with systems.
The [a href="http://docs.oracle.com/javase/8/docs/api/java/time/Clock.html" style="font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; font-size: 18px; color: rgb(117, 21, 144); -webkit-transition: color 0.3s; transition: color 0.3s; white-space: pre-wrap; word-wrap: break-word;"]Clock class gives us the ability to get the current date/time from the system clock with a specific time-zone. Although there are date-time classes that have a
now() method returning the current date-time, the system clock uses the default time-zone. The
Clock class allows us to get the system time with a given time-zone. We can then plug the
Clock instance into other classes where we want to get the current time using a given time-zone.
We can now re-work our previous example of creating an
OffsetTime object using a
Clockinstance to set the desired time-zone:
Clock class also allows us to specify how it ‘ticks’, meaning we can have the time returned from the
Clock instance ticking on whole minutes or seconds
The Instant class is used to capture the current ‘instant’ in time. The
Instant class is useful for obtaining event timestamps and also has nanosecond precision.
Instant class has several other methods for adjusting an
Instant instance such as adding/subtracting time or converting to a
The Period class represents an arbitrary amount of time in years, months, or days.
Period objects can be particularly useful for adding/subtracting time to/from a date. For example:
Period class also offers a static method
Period.between that is great for determining elapsed time between dates:
Period.between method requires types of
LocalDate, the date-time classes in the
java.time package offer a
toLocalDate() method that returns a
LocalDate from the date-time instance.
The Duration class represents arbitrary amounts of time in hours, minutes or seconds. The useage pattern for
Duration is similar to that of the
Period class. Here are examples of adjusting time objects using the
Duration class also has a
between method for determing the amount of time (hours based) between time objects:
Duration.between method requires types of
Temporal. All of the time and date-time classes in the
java.time package implement the
This wraps up our brief tour of the new Date API in Java 8. Hopefully you can see the promise in the new Date API for working with dates and times. In future posts I plan to continue coverage of the new features found in Java 8. Thanks for your time.
Published at DZone with permission of Bill Bejeck , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.