Java Reference
In-Depth Information
Listing 12-21. Multiple Ways of Getting the LocalTime from a ZonedDateTime
// LocalTimeFromZonedDateTime.java
package com.jdojo.datetime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalQueries;
public class LocalTimeFromZonedDateTime {
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() methdo 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);
}
}
lt1: 22:51:01.925
lt2: 22:51:01.925
lt3: 22:51:01.925
lt4: 22:51:01.925
lt5: 22:51:01.925
Search WWH ::




Custom Search