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

Java examples for Date Time:Duration

Description

Difference in Adding a Period and Duration to a Datetime

Demo Code

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(2019, 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);
  }//  w  w w .  ja v a2  s.  c om
}

Result


Related Tutorials