Re: returns wrong date during DST changes in "Europe/Paris" timezone
Nadh wrote:
System clock is set to "Europe/Paris" timezone. Creating a Calendar
object with the following details gives me wrong date.
public class TimeTest
{
public static void main(String[] args) throws Exception
{
Calendar ca = new GregorianCalendar( 2009,2,29,2,0,0);
System.out.println( "March DST : " + ca.getTime());
}
}
Expected output : Sun Mar 29 02:00:00 CEST/CST 2009
Actual output : Sun Mar 29 03:00:00 CEST/CST 2009
Unfortunately it is your expectations which are incorrect, Java is returning the
correct date/time. The time you requested never actually existed, there is a
hole in the space/time continuum at that instant. The clocks went from 01:59:59
CET to 03:00:00 CEST so requesting 02:00:00 29/03/2009 CEST makes no sense, I
presume Java sets the clock to the nearest possible valid time.
Exactly the same happens at 01:00:00 in the Europe/London timezone (we change at
the same instant as central Europe, so it's one hour earlier in wallclock time)
so here 01:00:00-01:59:59 BST disappear into a discontinuity in the fabric of
time.
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class DSTChange {
public static void main(String[] args) {
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Paris"));
Calendar ca = new GregorianCalendar( );
ca.set(2009,2,29,1,59,59);
System.out.println( "March before DST : " + ca.getTime());
ca.set(2009,2,29,2,0,0);
System.out.println( "March magic DST : " + ca.getTime());
ca.set(2009,2,29,3,0,0);
System.out.println( "March normal DST : " + ca.getTime());
TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
ca = new GregorianCalendar();
ca.set(2009,2,29,0,59,59);
System.out.println( "March before DST : " + ca.getTime());
ca.set(2009,2,29,1,0,0);
System.out.println( "March magic DST : " + ca.getTime());
ca.set(2009,2,29,2,0,0);
System.out.println( "March normal DST : " + ca.getTime());
}
}
Output:
March before DST : Sun Mar 29 01:59:59 CET 2009
March magic DST : Sun Mar 29 03:00:00 CEST 2009
March normal DST : Sun Mar 29 03:00:00 CEST 2009
March before DST : Sun Mar 29 00:59:59 GMT 2009
March magic DST : Sun Mar 29 02:00:00 BST 2009
March normal DST : Sun Mar 29 02:00:00 BST 2009
--
Nigel Wade