简体   繁体   中英

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 . I am using the below code snippet which I have modified from another found here on Stackoverflow while researching this subject; this code snippet will return: 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:

aCalendar.set(Calendar.HOUR, 0);

If I modify it as follows I get 1pm:

aCalendar.set(Calendar.HOUR, 0);

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

From the Java Docs

HOUR Field number for get and set indicating the hour of the morning or afternoon.

You'll want to use HOUR_OF_DAY instead

HOUR_OF_DAY Field number for get and set indicating the hour of the day.

Or set the AM_PM accordingly...

AM_PM Field number for get and set indicating whether the HOUR is before or after noon.

In any case, take the time to consult the Java Docs

Some example code using 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.

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. That moment may or may not be 00:00:00 in local time (because of Daylight Saving Time or other anomalies).

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 date = startOfPreviousMonth.toDate();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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