問(wèn)題描述
我正在嘗試從數(shù)據(jù)庫(kù)中獲取一些信息,然后使用該信息來(lái)獲取一些統(tǒng)計(jì)信息.
我想獲取基于小時(shí)間隔的統(tǒng)計(jì)信息,因此我創(chuàng)建了一個(gè)由兩個(gè) Integer
小時(shí)和數(shù)據(jù)組成的 HashSet
.
為了獲得正確的時(shí)間,我需要從數(shù)據(jù)庫(kù)中獲取時(shí)間.因此我需要?jiǎng)?chuàng)建某種數(shù)據(jù)/日歷對(duì)象.
現(xiàn)在由于 Date
已被棄用,我需要找到一種新的方法來(lái)設(shè)置時(shí)間.
有誰(shuí)知道我如何做到這一點(diǎn)?
到目前為止,此解決方案有效:
日歷時(shí)間 = Calendar.getInstance();time.setTime(new Date(2012, 11, 12, 8, 10));int hour = time.get(Calendar.HOUR);System.out.println(小時(shí));
但如上所述日期已被棄用,所以我想學(xué)習(xí)正確"的方法.
使用java.util.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(日歷.MILLISECOND, 0);
或使用 Joda Time http://joda-time.sourceforge.net/.p>
I am trying to get some information out of a database and then using that information to get some statistics.
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.
Using the 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);
Or use Joda Time http://joda-time.sourceforge.net/.
這篇關(guān)于設(shè)置和獲取小時(shí)、分鐘、秒的正確方法的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!