Java LocalDateTime - Get Hour, Minute and Second

LocalDateTime class provides below APIs to get Hour, Minute, Second from LocalDateTime.
  • 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.
import java.time.LocalDateTime;

/**
 * Program to demonstrate LocalDateTime Class APIs.
 * @author javaguides.net
 *
 */

public class LocalDateTimeExample {

    public static void main(String[] args) {
        getHourMinuteSecondfromLocalDateTime();
    }

    private static void getHourMinuteSecondfromLocalDateTime() {
        LocalDateTime dateTime = LocalDateTime.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 : 12
Minute : 48
Second : 2
Nano : 229000000


Comments