問題描述
我正在嘗試從數據庫中獲取一些信息,然后使用該信息來獲取一些統計信息.
我想獲取基于小時間隔的統計信息,因此我創建了一個由兩個 Integer
小時和數據組成的 HashSet
.
為了獲得正確的時間,我需要從數據庫中獲取時間.因此我需要創建某種數據/日歷對象.
現在由于 Date
已被棄用,我需要找到一種新的方法來設置時間.
有誰知道我如何做到這一點?
到目前為止,此解決方案有效:
日歷時間 = Calendar.getInstance();time.setTime(new Date(2012, 11, 12, 8, 10));int hour = time.get(Calendar.HOUR);System.out.println(小時);
但如上所述日期已被棄用,所以我想學習正確"的方法.
使用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/.
這篇關于設置和獲取小時、分鐘、秒的正確方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!