简体   繁体   English

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

[英]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 Integer s hour and data. 我想基于一个小时的间隔获得统计数据,因此我创建了一个由两个Integer数小时和数据组成的HashSet

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. 但如上所述,日期已被弃用,所以我想学习“正确”的方法。

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/

Getting Date-Time From Database 从数据库获取日期时间

Getting date-time from a database has been addressed in hundreds of answers. 从数据库中获取日期时间已在数百个答案中得到解决。 Please search StackOverflow. 请搜索StackOverflow。 Focus on java.sql.Timestamp . 专注于java.sql.Timestamp

To address the topic of your Question's title, read on. 要解决问题标题的主题,请继续阅读。

Joda-Time 乔达时间

Far easier if you use either Joda-Time or the java.time package bundled with Java 8 (inspired by Joda-Time). 如果您使用Joda-Time或与Java 8捆绑的java.time包(受Joda-Time启发),则会容易得多。 The java.util.Date & .Calendar classes bundled with Java are notoriously troublesome, confusing, and flawed. 与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 juDate to Joda-Time, pass the Date object to constructor of Joda-Time DateTime. 要从juDate转到Joda-Time,请将Date对象传递给Joda-Time DateTime的构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM