How to get the current hour with new Date-Time API in Java 8?

情到浓时终转凉″ 提交于 2019-12-22 03:54:06

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/25539891/how-to-get-the-current-hour-with-new-date-time-api-in-java-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!