Java - Get the LocalTime from a ZonedDateTime.

Description

Get the LocalTime from a ZonedDateTime.

Demo

import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalQueries;

public class Main {
  public static void main(String[] args) {
    ZonedDateTime zdt = ZonedDateTime.now();

    // Use the toLocalTime() method of the ZonedDateTime class (preferred)
    LocalTime lt1 = zdt.toLocalTime();

    // Use the from() method of the LocalTime class
    LocalTime lt2 = LocalTime.from(zdt);

    // Use the localTime() query
    LocalTime lt3 = zdt.query(TemporalQueries.localTime());

    // Use the LocalTime::from method as a query
    LocalTime lt4 = zdt.query(LocalTime::from);

    // Get all time components and construct a LocalTime
    int hours = zdt.getHour();
    int minutes = zdt.getMinute();
    int seconds = zdt.getSecond();
    int nanos = zdt.getNano();
    LocalTime lt5 = LocalTime.of(hours, minutes, seconds, nanos);

    // Print all LocalTimes
    System.out.println("lt1: " + lt1);
    System.out.println("lt2: " + lt2);
    System.out.println("lt3: " + lt3);
    System.out.println("lt4: " + lt4);
    System.out.println("lt5: " + lt5);
  }//from  w ww .  j av  a2 s. c  o m
}

Result

Related Topic