设置和获取小时、分钟、秒的正确方法

The correct way to set and get hour, minutes, sec(设置和获取小时、分钟、秒的正确方法)
本文介绍了设置和获取小时、分钟、秒的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试从数据库中获取一些信息,然后使用该信息来获取一些统计信息.

我想获取基于小时间隔的统计信息,因此我创建了一个由两个 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 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.

解决方案

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

这篇关于设置和获取小时、分钟、秒的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Month is not printed from a date - Java DateFormat(月份不是从日期打印的 - Java DateFormat)
java get week of year for given a date(java获取给定日期的一年中的一周)
Get the number of days, weeks, and months, since Epoch in Java(获取自 Java 中的 Epoch 以来的天数、周数和月数)
Is there a good way to get the date of the coming Wednesday?(有什么好方法可以得到即将到来的星期三的日期吗?)
Calendar view for Android GingerBread and before (APIlt;11)(Android GingerBread 及之前的日历视图 (API11))
How to save and retrieve Date in SharedPreferences(如何在 SharedPreferences 中保存和检索日期)