Java ZonedDateTime

Jakob Jenkov
Last update: 2015-06-28

The ZonedDateTime class in the Java 8 date time API represents a date and time with time zone information. This could be the start of a specific event somewhere in the world, like a conference, rocket launch etc.

The ZonedDateTime class is immutable. This means that all methods performing calculations on a ZonedDateTime object returns a new ZonedDateTime instance.

Creating a ZonedDateTime Object

You can create a ZonedDateTime object in several ways. The first way is to call the now() method of the ZonedDateTime class. Here is an example of creating a ZonedDateTime object using the now() method:

ZonedDateTime zonedDateTime = ZonedDateTime.now();

Another way to create a ZonedDateTime object is to use the of() method which can create a ZonedDateTime object from a concrete date and time. Here is an example of creating a ZonedDateTime object using the of() method:

ZoneId zoneId = ZoneId.of("UTC+1");

ZonedDateTime zonedDateTime2 =
    ZonedDateTime.of(2015, 11, 30, 23, 45, 59, 1234, zoneId);

Accessing Date and Time of a ZonedDateTime

You can access the date and time fields of a ZonedDateTime instance using one of the following methods:

  • getYear()
  • getMonth()
  • getDayOfMonth()
  • getDayOfWeek()
  • getDayOfYear()
  • getHour()
  • getMinute()
  • getSecond()
  • getNano()

Here is an example accessing the year of a ZonedDateTime:

int year = ZonedDateTime.now().getYear();

Some of these methods return an enum, and others return an int. From the enums you can return an int representation of their value using their getValue() methods. For instance:

int month = ZonedDateTime.now()
              .getMonth().getValue();

Date and Time Calculations

The ZonedDateTime class contains a set of methods used for date time calculations. Some of these methods are:

  • plusYears()
  • plusMonths()
  • plusDays()
  • plusHours()
  • plusMinutes
  • plusSeconds
  • plusNanos()
  • minusYears()
  • minusMonths()
  • minusDays()
  • minusHours()
  • minusMinutes
  • minusSeconds
  • minusNanos()

Be aware that calculations that span across the daylight savings changes (start or end) may not give the result you expect! An alternative is to use a Period instance, like this:

ZonedDateTime newZoneDateTime =
    previousDateTime.plus(Period.ofDays(3));

This should result in a more correct calculation.

Time Zones

The time zones are represented by the ZoneId class as shown in the earlier example. You can create a ZoneId object using the ZoneId.now() method. Here is an example:

ZoneId zoneId = ZoneId.of("UTC+1");

The parameter passed to the of() method is the ID of the time zone to create a ZoneId for. In the example above the ID is "UTC+1" which is an offset from UTC (Greenwich) time. You can find the UTC offset for the desired time zone and create an ID matching it by combining "UTC" with the offset (e.g. "+1" or "-5").

You can also use another type of time zone id which consists of the name of the location where the time zone is active. Here is an example:

ZoneId zoneId2 = ZoneId.of("Europe/Copenhagen");

ZoneId zoneId3 = ZoneId.of("Europe/Paris");

Jakob Jenkov

Featured Videos

Java Generics

Java ForkJoinPool

P2P Networks Introduction



















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next