Duplicate :
|
|
Duplicate :
|
|
Relates :
|
|
Relates :
|
Name: skT88420 Date: 06/22/99 The roll methods in the GregorianCalendar class do not always function properly when rolling October 31 forward. This problem occurs when using the boolean version of the roll method, public void roll(int field, boolean up), and when using the alternate version, public void roll(int field, int amount) to roll Calendar.DAY_OF_YEAR. However, the problem only seems to occur when rolling the day of year forward and not when rolling it backward. The following code should demonstrate my problem: import java.util.*; import java.text.DateFormat; public class DateTest { public static void main(String args[]) { GregorianCalendar current = new GregorianCalendar(1999,8,29); for (int i=0; i<75; i++) { current.roll(Calendar.DAY_OF_YEAR, true); System.out.println(DateFormat.getDateInstance(DateFormat.LONG).format(current.getTime())); } } } Notice that October 31, 1999 is printed twice. (Review ID: 84655) ====================================================================== Name: krT82822 Date: 01/30/2000 (see also 4191164, 4248500) I have observed this bug in JDK 1.1.7, JDK 1.1.8, JDK 1.2.1, JDK 1.2.2. The Calendar/GregorianCalendar code is very broken around DST switchovers. I'll include a sample to illustrate what I mean. Basically, calling Calendar.set(SECOND, 0) sets the underlying long from EDT to EST!!! As documented by others there are no constructors that allow creation of times around these dst switchovers, or explicit api calls to say Calendar.setIsDST(true); Without these it is impossible to disambiguate 1:30 am on Oct 31, 1999 from the EST or EDT versions. Here's the code: To see the bug, compile and execute and see that after you set the seconds field to 0, the calendar time switches from EDT to EST, a difference of ONE hour!!! -- import java.util.*; public class DSTTest { public static void main(String args[]) { Date d = new Date(); GregorianCalendar cal = new GregorianCalendar(1999, 9, 31, 1, 15); d = cal.getTime(); System.out.println("Date = " + d); // subtract one hour long val = d.getTime() - (60*1000*60); d = new Date(val); System.out.println("Date after subtracting = " + d); cal.setTime(d); System.out.println("Calendar after setTime = " + cal.getTime() + " Long= " + cal.getTime().getTime()); cal.set(Calendar.SECOND, 0); System.out.println("Calendar after set(SECOND,0) = " + cal.getTime() + " Long = " + cal.getTime().getTime()); } } (Review ID: 100459) ======================================================================
|