java 设置和获取小时、分钟、秒的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13343524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The correct way to set and get hour, minutes, sec
提问by Marc Rasmussen
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.
我想根据时间间隔获取统计信息,因此我创建了一个HashSet
由两个Integer
小时和数据组成的数据。
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.
现在由于Date
已被弃用,我需要找到一种设置时间的新方法。
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.
但如上所述日期已被弃用,所以我想学习“正确”的方法来做到这一点。
回答by Aleksandr M
Using the java.util.Calendar
使用 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/.
或者使用 Joda Time http://joda-time.sourceforge.net/。
回答by Basil Bourque
Getting Date-Time From Database
从数据库获取日期时间
Getting date-time from a database has been addressed in hundreds of answers. Please search StackOverflow. Focus on java.sql.Timestamp
.
从数据库中获取日期时间已在数百个答案中得到解决。请搜索 StackOverflow。专注于java.sql.Timestamp
.
To address the topic of your Question's title, read on.
要解决问题标题的主题,请继续阅读。
Joda-Time
乔达时间
Far easier if you use either Joda-Timeor the java.time package bundled with Java 8 (inspired by Joda-Time). The java.util.Date & .Calendar classes bundled with Java are notoriously troublesome, confusing, and flawed.
如果您使用Joda-Time或 Java 8 捆绑的 java.time 包(受 Joda-Time 启发),则容易得多。与 Java 捆绑在一起的 java.util.Date 和 .Calendar 类是出了名的麻烦、混乱和有缺陷。
Time zone is crucial. Unlike java.util.Date, both Joda-Time and java.time assign a time zone to their date-time objects.
时区至关重要。与 java.util.Date 不同,Joda-Time 和 java.time 都为它们的日期时间对象分配一个时区。
Here is some example code to show multiple ways to set the time-of-day on a Joda-Time 2.5 DateTime object.
下面是一些示例代码,用于展示在 Joda-Time 2.5 DateTime 对象上设置时间的多种方法。
DateTimeZone zoneMontreal = DateTimeZone.forID( "America/Montreal" ); // Specify a time zone, or else the JVM's current default time zone will be assigned to your new DateTime objects.
DateTime nowMontreal = DateTime.now( zoneMontreal ); // Current moment.
DateTime startOfDayMontreal = nowMontreal.withTimeAtStartOfDay(); // Set time portion to first moment of the day. Usually that means 00:00:00.000 but not always.
DateTime fourHoursAfterStartOfDayMontreal = startOfDayMontreal.plusHours( 4 ); // You can add or subtract hours, minutes, and so on.
DateTime todayAtThreeInAfternoon = nowMontreal.withTime(15, 0, 0, 0); // Set a specific time of day.
Converting
转换
If you absolutely need a java.util.Date object, convert from Joda-Time.
如果您绝对需要 java.util.Date 对象,请从 Joda-Time 转换。
java.util.Date date = startOfDayMontreal.toDate();
To go from j.u.Date to Joda-Time, pass the Date object to constructor of Joda-Time DateTime.
要从 juDate 转到 Joda-Time,请将 Date 对象传递给 Joda-Time DateTime 的构造函数。