Java 8 - LocalTime getHour(), getMinute(), getSecond() and getNano() Example

LocalTime class provides below APIs to get Hour, Minute, Second from LocalTime.
  • int getHour() - Gets the hour-of-day field.
  • int getMinute() - Gets the minute-of-hour field.
  • int getNano() - Gets the nano-of-second field.
  • int getSecond() - Gets the second-of-minute field.
The java.time.LocalTime class is an immutable class which represents a time without time-zone information.

Read more about LocalTime class with an example at https://www.javaguides.net/2018/08/java-8-localtime-class-api-guide.html.

LocalTime getHour(), getMinute(), getSecond() and getNano() Example

import java.time.LocalTime;

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

    private static void getHourMinuteSecondfromLocalTime() {
        LocalTime localTime = LocalTime.now();
        System.out.println("Gets the hour-of-day field : " + localTime.getHour());
        System.out.println("Gets the minute-of-hour field : " + localTime.getMinute());
        System.out.println("Gets the second-of-minute field : " + localTime.getSecond());
       System.out.println("Gets the nano-of-second field : " + localTime.getNano());
    }
}
Output:
Gets the hour-of-day field : 17
Gets the minute-of-hour field : 40
Gets the second-of-minute field : 30
Gets the nano-of-second field : 182000000

Reference


Comments