Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

java - 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.

I want to get statistics based on an interval of hours, therefore I have a created a HashSet made up of two Integers 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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/.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...