How to Get Hour, Minute, Second from ZonedDateTime in Java



ZonedDateTime class provides below APIs to get Hour, Minute, Second from ZonedDateTime class.
  • int getHour() - Gets the hour-of-day field.
  • int getMinute() - Gets the minute-of-hour field.
  • int getSecond() - Gets the second-of-minute field.
  • int getNano() - Gets the nano-of-second field.
import java.time.ZonedDateTime;

/**
 * Program to demonstrate ZonedDateTime Class APIs.
 * @author javaguides.net
 *
 */
public class ZonedDateTimeExample {
 
    public static void main(String[] args) {
       getHourMinuteSecondfromZonedDateTime();
     }

     private static void getHourMinuteSecondfromZonedDateTime() {
        ZonedDateTime dateTime = ZonedDateTime.now();
        System.out.println("Hour : " + dateTime.getHour());
        System.out.println("Minute : " + dateTime.getMinute());
        System.out.println("Second : " + dateTime.getSecond());
        System.out.println("Nano : " + dateTime.getNano());
    }
}
Output:
Hour : 11
Minute : 23
Second : 8
Nano : 5000000

Comments