Java 8 Time – How to Get the Current Hour with New Date-Time API in Java 8

javajava-8java-time

Before Java 8 the common method was the Calendar API:

Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR_OF_DAY);

How can I get the current hour, but using the new Date-Time API provided in Java 8?

Best Answer

This all depends, but something like...

LocalTime now = LocalTime.now();
System.out.println(now.getHour());

Should work just fine...

Take a closer look at LocalTime#getHour for more details

Related Question