ZonedDateTime and Duration

Description

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.

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.Duration;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
// w w w . j  a  va 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.





















Home »
  Java Date Time »
    Tutorial »




Java Date Time Tutorial