如何使用Java 8中的新Date-Time API获取当前小时? [英] How to get the current hour with new Date-Time API in Java 8?

查看:1333
本文介绍了如何使用Java 8中的新Date-Time API获取当前小时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java 8之前,常见的方法是日历 API:

Before Java 8 the common method was the Calendar API:

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

如何获得当前小时数,但使用新的日期时间API Java 8 中提供?

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

推荐答案

这完全取决于......但

This all depends, but something like...

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

应该可以正常工作......

Should work just fine...

仔细看看 LocalTime# getHour 了解更多详情

Take a closer look at LocalTime#getHour for more details

这篇关于如何使用Java 8中的新Date-Time API获取当前小时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆