Vamsi Challa
Vamsi Challa

Reputation: 11109

Java Calendar - Set Custom date and time

I am trying to set custom date and time for Calendar object in java. I have to create a date which is 1 day more than current date, and time should be set as "X" (some time like 05:00:00).

Here is what i did till now:

Calendar tomorrow = Calendar.getInstance();
tomorrow.add(Calendar.DATE, 1);

wake_up_time = "05:00:00";

Date date_wake_up_time = new SimpleDateFormat("HH:mm:ss").parse(wake_up_time);
Calendar cal_wake_up_time = Calendar.getInstance();
cal_wake_up_time.setTime(date_wake_up_time);

tomorrow.set(Calendar.HOUR, cal_wake_up_time.get(Calendar.HOUR));
tomorrow.set(Calendar.MINUTE, cal_wake_up_time.get(Calendar.MINUTE));
tomorrow.set(Calendar.SECOND, cal_wake_up_time.get(Calendar.SECOND));

Now when i do tomorrow.getTimeInMillis() i should be getting a constant long value, since for a particular day, i am setting the time as constant. But, when i run this code, the long value keeps on changing.

Tomorrow Date: 3
Tomorrow Hour: 5
Tomorrow Minute: 0
Tomorrow Second: 0
Tomorrow's Time: 1467522000711

Tomorrow Date: 3
Tomorrow Hour: 5
Tomorrow Minute: 0
Tomorrow Second: 0
Tomorrow's Time: 1467522000169

and more over, why is the time value decreasing with each iteration? What am i doing wrong here? How do i set a Calendar to tomorrow's X time?

Upvotes: 0

Views: 2432

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 338201

java.time

You are using outmoded classes, now supplanted by the java.time classes.

Time zone

Time zone is crucial in determine a date. The JVM‘s current default can change at any moment. So always specify the desired/expected time zone explicitly.

ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime now = ZonedDateTime.now( zoneId );

The Local… classes have no time zone. These can be used as building blocks to assemble our desired value of “tomorrow” and “at 5 AM”.

A new LocalTime object defaults to all zeros. So no need to clear minutes, seconds, and fractional second. And so no problem like that which tripped you up with Calendar.

LocalDate tomorrow = now.toLocalDate().plusDays( 1 );
LocalTime fiveAm = LocalTime.of( 5 , 0 );
ZonedDateTime tomorrowAt0500 = ZonedDateTime.of( tomorrow , fiveAm , zoneId );

Convert

If you must have a java.util.Calendar object for use in old code, convert from the java.time type. Look for new methods added to the old classes.

java.util.Calendar cal = java.util.GregorianCalendar.from( tomorrowAt0500 );

Upvotes: 0

Krzysztof Krasoń
Krzysztof Krasoń

Reputation: 27466

If you are doing:

Calendar cal_wake_up_time = Calendar.getInstance();

Then each time the calendar will have a different time stored so you will have different milliseconds value for it.

If so then you need to clear the values that you are not setting using, either by doing a clear() in the beginning or doing it per field using clear(int field) (e.g. clear(Calendar.MILLISECOND)):

tomorrow.clear(Calendar.MILLISECOND);

Upvotes: 1

Vamsi Challa
Vamsi Challa

Reputation: 11109

The code in the question is working just fine. The problem was, i didn't set Milliseconds to 0 or a constant (tomorrow.set(Calendar.SECOND, 0);) The change was only milliseconds, which i overlooked. This also gave me the assumption that the long value was decreasing each time i executed the code.

Upvotes: 0

Related Questions