Java getHours(),getMinutes()和getSeconds() [英] Java getHours(), getMinutes() and getSeconds()

查看:796
本文介绍了Java getHours(),getMinutes()和getSeconds()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 getHours() getMinutes() getSeconds() / code>都在Java中弃用,它们被替换为 Calendar.HOUR_OF_DAY Calendar.MINUTE Calendar.SECOND

这些事实上将返回该时刻的分钟和秒。但是,我想从Date变量中检索小时和分钟。例如,

These will in fact return the hour, minute and second for that particular moment. However, I would want to retrieved the hours and minutes from a Date variable. For instance,

说从数据库检索的时间是

say the time retrieved from database is

time = Thu Jan 01 09:12:18 CET 1970;

int hours = time.getHours();
int minutes = time.getMinutes();
int seconds = time.getSeconds();

通过检索小时,分钟和秒数,我得到

By retrieving the hours, minutes, and seconds, I get

hours = 9
minutes = 12
seconds = 18

那么,我该如何使用Calendar来执行这个功能?虽然 getHours()已被弃用,但仍然有效。我仍然想知道是否有另一种选择。

So, how do I use Calendar for this function? Although the getHours() has been deprecated but it still worked. I would still like to know if there is an alternative to this.

推荐答案

尝试这样:

Calendar calendar = Calendar.getInstance();
calendar.setTime(yourdate);
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int minutes = calendar.get(Calendar.MINUTE);
int seconds = calendar.get(Calendar.SECOND);

这篇关于Java getHours(),getMinutes()和getSeconds()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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