Java ZonedDateTime convert to LocalDateTime

Description

Java ZonedDateTime convert to LocalDateTime

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;

public class Main {
  public static void main(String[] args) {
    // Get the current zoned date time for the system default time zone
    ZonedDateTime zdt1 = ZonedDateTime.now();
    System.out.println("Current zoned datetime:" + zdt1);
    /*  ww  w.  ja  v a 2 s.c o m*/
    // Perform some conversions on zoned date time
    LocalDateTime ldt2 = zdt1.toLocalDateTime();
    OffsetDateTime odt = zdt1.toOffsetDateTime();
    Instant i1 = zdt1.toInstant();
    System.out.println("To local datetime: " + ldt2);
    System.out.println("To offset datetime: " + odt);
    System.out.println("To instant: " + i1);
  }
}



PreviousNext

Related