简体   繁体   English

如何在Java中设置日期的最小小时数?

[英]How Can I Set Miniumum Hour for Date in Java?

I'm trying to get the date and time of the first day of the prior month; 我正在尝试获取上个月第一天的日期和时间; specifically as it is January I'm trying to get: Sun Dec 01 00:00:00 EST 2013 . 尤其是1月,我正尝试获取: Sun Dec 01 00:00:00 EST 2013 I am using the below code snippet which I have modified from another found here on Stackoverflow while researching this subject; 我在研究此主题时使用的是下面的代码段,这些代码段是我在Stackoverflow上的另一个代码段中修改的; this code snippet will return: Sun Dec 01 12:00:00 EST 2013 . 该代码段将返回: Sun Dec 01 12:00:00 EST 2013 I do not understand why setting the minimum hour for the 1st in fact returns noon. 我不明白为什么设置第一时间的最低时数实际上会返回中午。

Calendar aCalendar = Calendar.getInstance();
// add -1 month to current month
aCalendar.add(Calendar.MONTH, -1);
// set DATE to 1, so first date of previous month
//aCalendar.set(Calendar.DATE, 1);                        
aCalendar.set(Calendar.DATE, aCalendar.getActualMinimum(Calendar.DAY_OF_MONTH));
aCalendar.set(Calendar.HOUR, aCalendar.getActualMinimum(Calendar.HOUR));
aCalendar.set(Calendar.MINUTE, aCalendar.getActualMinimum(Calendar.MINUTE));
aCalendar.set(Calendar.SECOND, aCalendar.getActualMinimum(Calendar.SECOND));
Date firstDateOfPreviousMonth = aCalendar.getTime();

If I modify the following line as shown and set 0 I get the same result: 如果我如图所示修改以下行并将其设置为0,则得到相同的结果:

aCalendar.set(Calendar.HOUR, 0);

If I modify it as follows I get 1pm: 如果按如下所示进行修改,则会得到1pm:

aCalendar.set(Calendar.HOUR, 0);

Result: Sun Dec 01 13:00:00 EST 2013 结果: Sun Dec 01 13:00:00 EST 2013

From the Java Docs 从Java文档

HOUR Field number for get and set indicating the hour of the morning or afternoon. HOUR get和set的字段编号,指示上午或下午的时间。

You'll want to use HOUR_OF_DAY instead 您将改为使用HOUR_OF_DAY

HOUR_OF_DAY Field number for get and set indicating the hour of the day. HOUR_OF_DAY get和set的字段号,指示一天中的小时。

Or set the AM_PM accordingly... 或相应地设置AM_PM ...

AM_PM Field number for get and set indicating whether the HOUR is before or after noon. AM_PM用于获取和设置的字段号,指示HOUR是在中午之前还是之后。

In any case, take the time to consult the Java Docs 无论如何,请花一些时间查阅Java文档

Some example code using Joda-Time 2.3. 一些使用Joda-Time 2.3的示例代码。

Using default time zone, we create a DateTime instance of now (current moment), go back to previous month (in a smart manner, compensating for short month like February), get the first of month by asking for "minimum value", and then get first moment of that first-of-month day. 使用默认时区,我们创建一个现在(当前时刻)的DateTime实例,返回上个月(以一种聪明的方式,补偿像2月这样的短月份),通过询问“最小值”来获得该月的第一天,并且然后获得该月第一天的第一刻。

org.joda.time.DateTime startOfPreviousMonth = new org.joda.time.DateTime().minusMonths(1).dayOfMonth().withMinimumValue().withTimeAtStartOfDay();

Note the method withTimeAtStartOfDay to get the first moment of the day. 请注意withTimeAtStartOfDay方法来获取一天的第一时刻。 That moment may or may not be 00:00:00 in local time (because of Daylight Saving Time or other anomalies). 该时间可能是当地时间的00:00:00,也可能不是00:00:00(由于夏令时或其他异常)。

Using explicit time zone (almost always a better practice)… 使用明确的时区(几乎总是更好的做法)…

DateTimeZone timeZone = DateTimeZone.forId( "Europe/Paris" ); // "Asia/Kolkata" etc.
org.joda.time.DateTime startOfPreviousMonth = new org.joda.time.DateTime( timeZone ).minusMonths(1).dayOfMonth().withMinimumValue().withTimeAtStartOfDay();

Get a string… 得到一个字符串...

String string = startOfPreviousMonth; // or startOfPreviousMonth.toString();

To convert back to a java.util.Date instance for communicating with other libraries… 要转换回java.util.Date实例以与其他库进行通信……

java.util.Date date = startOfPreviousMonth.toDate();

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

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