The set method only sets the year, month, date, hour and minute fields of the GregorianCalendar. Therefore seconds, milliseconds will be retained from when you created the object, you need to clear all the time fields first.
Sets the values for the fields year, month, date, hour, and minute. Previous values of other fields are retained. If this is not desired, call clear first.
However it is probably easier to just use the GregorianCalendar constructor.
Joined: Jul 29, 2003
Hi Jason, Thanks a bunch. Your explaination helped.
A related question: If I use the default GregorianCalendar constructor, I get the current date and the current time. What is the simplest way to get the current date at the previous midnight? For example, today is April 2, 2006, and it is about 7 pm. But I want today at 12:01 am minus one minute, in other words the beginning of today.
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Depending how you're displaying the result, this may or may not be noticeable, but milliseconds are part of the state of the GregorianCalendar, which probably should be zeroed out with the rest of them. The fields AM_PM and HOUR are redundant, their values implied by HOUR_OF_DAY, so you can safely skip them.
This is misleading - getInstance() is in the Calendar class, and static. In my opinion it's clearer to either use Calendar.getInstance(), or just use new GregorianCalendar().
Should be unnecessary - a newly created Calendar is already at the current time.
This appears to be for some problem other than the one Marilyn described.
"I'm not back." - Bill Harding, Twister
Marilyn de Queiroz
Joined: Jul 22, 2000
Ok, thanks. It just seems like there should be a simpler way than calling set() four times, but if there isn't, there isn't.
Joined: Jan 30, 2000
Simpler? Sure, but not from Sun. Calendar and GregorianCalendar are among the more API's they've created. You seek Joda: