Java - Difference in Adding a Period and Duration to a Datetime

Description

Difference in Adding a Period and Duration to a Datetime

Demo

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;

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);
    Duration d1 = Duration.ofHours(24);

    // Add a period of 1 day and a duration of 24 hours
    ZonedDateTime zdt2 = zdt1.plus(p1);
    ZonedDateTime zdt3 = zdt1.plus(d1);

    System.out.println("Start Datetime: " + zdt1);
    System.out.println("After 1 Day period: " + zdt2);
    System.out.println("After 24 Hours duration: " + zdt3);
  }//from w ww.  j  a  v  a2s  .  c  o m
}

Result

Related Topic