ZonedDateTime and Period

Description

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.

Example

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;
//from  ww w  .  j a v  a2  s  . co  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);
    Period p1  = Period.ofDays(1); 

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

The code above generates the following result.





















Home »
  Java Date Time »
    Tutorial »




Java Date Time Tutorial