Search code examples
javatimezonejdeveloperdst

Java Jdeveloper


When I run the following java code in JDeveloper, I get the wrong output. What can be the reasons for that?

    System.out.println(TimeZone.getTimeZone("Australia/Sydney").inDaylightTime(new Date()));
    System.out.println(TimeZone.getTimeZone("Europe/Moscow").inDaylightTime(new Date()));

I am expecting an output as: true false

But, I am getting it as: false true

PS: This is working correct for few other time zones like "America/Fort_Wayne" and "Europe/Berlin".


Solution

  • Since the use of new Date() is not fully following the principle of How to create a Minimal, Complete, and Verifiable example, here it is:

    public static void main(String[] args) {
        Date date = new Date(1444817534489L); // 2015-10-14 10:12:14.489 UTC
        printDate(date, "UTC");
        printDate(date, "Australia/Sydney");
        printDate(date, "Europe/Moscow");
        printDate(date, "Europe/Berlin");
        printDate(date, "America/Fort_Wayne");
    }
    private static void printDate(Date date, String timeZoneID) {
        TimeZone timeZone = TimeZone.getTimeZone(timeZoneID);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
        df.setTimeZone(timeZone);
        System.out.printf("%-20s %-30s inDaylightTime: %s%n", timeZoneID + ":",
                          df.format(date), timeZone.inDaylightTime(date));
    }
    

    Output

    UTC:                 2015-10-14 10:12:14.489 UTC    inDaylightTime: false
    Australia/Sydney:    2015-10-14 21:12:14.489 AEDT   inDaylightTime: true
    Europe/Moscow:       2015-10-14 13:12:14.489 MSK    inDaylightTime: false
    Europe/Berlin:       2015-10-14 12:12:14.489 CEST   inDaylightTime: true
    America/Fort_Wayne:  2015-10-14 06:12:14.489 EDT    inDaylightTime: true
    

    So, true false as you expected. If you saw different, maybe your date was different, and without printing it, how would you know?