正确的方式设置和获取小时,分钟,秒 [英] The correct way to set and get hour, minutes, sec
问题描述
我想从数据库中获取一些信息,然后使用该信息来获取一些统计信息。
I am trying to get some information out of a database and then using that information to get some statistics.
我想根据时间间隔获取统计信息,因此我创建了一个 HashSet
,由两个 Integer
的小时和数据组成。
I want to get statistics based on an interval of hours, therefore I have a created a HashSet
made up of two Integer
s hour and data.
为了得到正确的小时,我需要从数据库中获取时间。因此,我需要创建某种数据/日历对象。
In order to get the correct hour I need to get the time out of the database. Therefore I need to create some sort of data / calendar object.
现在由于日期
已被弃用,我需要找到一种新的方法来设置小时。
Now since Date
has been deprecated I need to find a new way to set the hours.
有没有人知道我可以如何解决这个问题?
Does anyone know how i can achive this?
此解决方案的工作原理:
So far this solution works:
Calendar time = Calendar.getInstance();
time.setTime(new Date(2012, 11, 12, 8, 10));
int hour = time.get(Calendar.HOUR);
System.out.println(hour);
但是如上所述日期已被弃用,所以我想学习正确的方式。
But as stated above date has been deprecated so I want to learn the "correct" way to do it.
推荐答案
使用 java.util.Calendar
Calendar c = Calendar.getInstance();
c.set(Calendar.DATE, 2);
c.set(Calendar.HOUR_OF_DAY, 1);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
或使用Joda Time http://joda-time.sourceforge.net/ 。
Or use Joda Time http://joda-time.sourceforge.net/.
这篇关于正确的方式设置和获取小时,分钟,秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!