I'm having a strange issue with abstract Calendar class using the GregorianCalendar method.
For some reason using "calendar.set" is returning a date from the previous day. See code below for example with comments on where it works fine, and where it goes wrong.
private Date checkDate(Date d, int hour, int minute, int sec, int milliSec)
{
// Test values for arguments
d = "Wed Apr 06 00:00:00 BST 2011";
hour = minute = sec = milliSec = 0;
Calendar calendar = new GregorianCalendar(sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]);
calendar.setTime(d);
// *** OK *** calendar.getTime() will display correctly here (Wed Apr 06 00:00:00 BST 2011)
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, sec);
calendar.set(Calendar.MILLISECOND, milliSec);
// *** NOT OK *** calendar.getTime() will display inncorrectly here (Tue Apr 05 01:00:00 BST 2011)
return calendar.getTime();
} Any ideas?
Thanks
Calendar calendar = new GregorianCalendar(sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]);
calendar.setTime(d);
Ok, so you have a calendar with its timezone set to UTC and set its time to
Wed Apr 06 00:00:00 BST 2011
.
Internally, this is mapped to
2011-04-05 23:00:00 UTC
(the date and time is adopted, the time zone is kept).
Setting the hour, minute, second and millisecond fields of the calendar to 0 will change it to
2011-04-05 00:00:00 UTC
.
If you now convert the calendar to a java.util.Date
object and print it while considering your local time zone (BST), the output will be:
2011-04-05 01:00:00 BST
.