Java Date Time - Java Zoned Date Time








ZonedDatetime

ZonedDateTime class represents a date time with time zone rules.

ZonedDateTime combines LocalDateTime and ZoneId.

The following shows how to create a ZonedDateTime from a LocalDateTime.

import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
//from   w  w w .  jav  a 2 s.  c  o m
public class Main {
  public static void main(String[] args) {
    ZoneId usCentral = ZoneId.of("America/Chicago");
    LocalDateTime localDateTime = LocalDateTime.of(2014, Month.MAY, 21, 9, 30);
    System.out.println(localDateTime);
    ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, usCentral);
    System.out.println(zonedDateTime);
  }
}

The code above generates the following result.





Note

A gap or overlap on the local timeline in a time zone may happen because of the Daylight Saving Time change.

When the clock is moved an hour forward or backward there would be a gap or overlap in time.

If the time falls in the middle of the gap, the time is moved forward by the same amount as the gap.

If the time falls in the middle of the overlap, the time is valid.

By default, the earlier is used. withEarlierOffsetAtOverlap() and withLaterOffsetAtOverlap() from ZonedDateTime let you select the desired zone offset if the time falls in the overlap.





Example 2

The following code shows ZonedDateTime with the time falling in the gap and overlap.

import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
/*from  w  ww .  jav  a 2 s.  c  om*/
public class Main {
  public static void main(String[] args) {
    ZoneId usChicago = ZoneId.of("America/Chicago");

    // 2014-03-09T02:30 did not exist in America/Chicago time zone
    LocalDateTime ldt = LocalDateTime.of(2014, Month.MARCH, 9, 2, 30);
    ZonedDateTime zdt = ZonedDateTime.of(ldt, usChicago);
    System.out.println(zdt);

    // 2013-10-03T01:30 existed twice in America/Chicago time zone
    LocalDateTime ldt2 = LocalDateTime.of(2013, Month.NOVEMBER, 3, 1, 30);
    ZonedDateTime zdt2 = ZonedDateTime.of(ldt2, usChicago);

    System.out.println(zdt2.withEarlierOffsetAtOverlap());
    System.out.println(zdt2.withLaterOffsetAtOverlap());

  }
}

The code above generates the following result.

Example 3

ZonedDateTime.ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) creates a ZonedDateTime from zone offset.

If the specified referred zone offset is invalid, the earlier zone offset for the overlap is used.

-07:00 is not valid, the earlier offset -05:00 is used.

import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
//from  w w  w  .  j ava 2 s .c om
public class Main {
  public static void main(String[] args) {
    ZoneId  usChicago   = ZoneId.of("America/Chicago"); 
    ZoneOffset offset5  = ZoneOffset.of("-05:00"); 
    ZoneOffset offset6  = ZoneOffset.of("-06:00"); 
    ZoneOffset offset7  = ZoneOffset.of("-07:00");

    LocalDateTime  ldt = LocalDateTime.of(2012, Month.NOVEMBER,  4, 1, 30); 
    ZonedDateTime zdt5   = ZonedDateTime.ofLocal(ldt,  usChicago, offset5); 
    ZonedDateTime zdt6   = ZonedDateTime.ofLocal(ldt,  usChicago, offset6); 
    ZonedDateTime zdt7   = ZonedDateTime.ofLocal(ldt,  usChicago, offset7); 

    System.out.println("With offset  "  + offset5 + ": "  + zdt5); 
    System.out.println("With offset  "  + offset6 + ": "  + zdt6); 
    System.out.println("With offset  "  + offset7 + ": "  + zdt7);

  }
}

The code above generates the following result.

Example 4

The following code shows how to content ZonedDateTime to local and offset date, time and datetime.

import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
//w  w  w  .  ja v  a 2  s .  co  m
public class Main {
  public static void main(String[] args) {
    ZonedDateTime zdt1   = ZonedDateTime.now(); 
    System.out.println("Current zoned  datetime:" + zdt1);

    LocalDateTime  ldt = LocalDateTime.of(2012, Month.MARCH,  11,   7, 30);

    ZoneId  usCentralZone = ZoneId.of("America/Chicago"); 
    ZonedDateTime zdt2   = ZonedDateTime.of(ldt, usCentralZone); 
    System.out.println(zdt2);
  }
}

The code above generates the following result.

ZonedDateTime and Duration

When you add a duration of a day, it will always add 24 hours, regardless of how many hours the day had (23, 24, or 25 hours).

23 and 25 hours happens when changing from normal time to day-light saving time or back. When entering the day-light saving time we lose an hour. When leaving the day-light saving time we get an hour extra.

On 2012-03-11T02:00, US Central time zone entered the day-light saving by moving clock forward by one hour, making 2012-03-11 a 23-hour day.

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
/*from  w  w  w.  j  av  a  2  s  .  c o m*/
public class Main {

  public static void main(String[] args) {
    ZoneId usCentral = ZoneId.of("America/Chicago");
    LocalDateTime ldt = LocalDateTime.of(2012, Month.MARCH, 10, 7, 30);
    ZonedDateTime zdt1 = ZonedDateTime.of(ldt, usCentral);

    Duration d1 = Duration.ofHours(24);

    ZonedDateTime zdt2 = zdt1.plus(d1);
    System.out.println(zdt2);
  }
}

The code above generates the following result.

ZonedDateTime and Period

When adding a period of one day to a ZonedDateTime, the date component changes to the next day without affecting the time, regardless of how many hours the day had (23, 24, or 25 hours).

23 and 25 hours happens when changing from normal time to day-light saving time or back. When entering the day-light saving time we lose an hour. When leaving the day-light saving time we get an hour extra.

On 2012-03-11T02:00, US Central time zone entered the day-light saving by moving clock forward by one hour, making 2012-03-11 a 23-hour day.

import java.time.LocalDateTime;
import java.time.Month;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
//  w ww .  j  a  va  2  s . c om
public class Main {

  public static void main(String[] args) {
    ZoneId  usCentral = ZoneId.of("America/Chicago");
    LocalDateTime  ldt = LocalDateTime.of(2012, Month.MARCH,  10,   7, 30); 
    ZonedDateTime zdt1   = ZonedDateTime.of(ldt, usCentral);
    Period p1  = Period.ofDays(1); 

    ZonedDateTime zdt2   = zdt1.plus(p1); 
    System.out.println(zdt2);
  }
}

The code above generates the following result.