从日期获得小时,分钟和秒? [英] Getting hours,minutes, and seconds from Date?

查看:93
本文介绍了从日期获得小时,分钟和秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Date 对象获取当前时,分和秒。

I want to get the current hour,minute, and second from the Date object.

这是我的代码:

Date date = new Date();
int hour = date.getHours();

但是我遇到了一种错误, getHours()部分无效。我得到不赞成使用Data类型的getHours()方法。这是什么意思,我如何获得小时,分钟和秒?

But I get a kind of error, the getHours() part doesnt work. I get a "The method getHours() from the type Data is deprecated". What does that mean, and how do I get the hour,minutes, and seconds?

推荐答案

使用 日历

Calendar cal = Calendar.getInstance();
cal.setTime(date);  
int hours = cal.get(Calendar.HOUR_OF_DAY);

来自日期 javadoc


已弃用。 从JDK版本1.1开始,由 Calendar.get(Calendar.MINUTE)代替。

返回该日期所表示的小时后的分钟数,以当地时区解释。返回的值在0到59之间。

Returns the number of minutes past the hour represented by this date, as interpreted in the local time zone. The value returned is between 0 and 59.

这篇关于从日期获得小时,分钟和秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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