This discussion is archived

# calculating time for different timezone, problems with daylight savings?

Currently Being Moderated
Hi all,

I try to convert my locale time to a different timezone (like a world clock does). This worked until Europe got "summer time". Then my time calculation went wrong. I just paste the following coding to give you a quick reproducable code.

I did a lot of googleing but nothing found so far. Also search here in the forum didn't help me solving it. So now I created a post on my own.

The output of the Java programm is the following (without the colored comments). I just entered these comments to show you where the calculation is right and where it goes wrong.
I have absolutely now idea about where the chase the error. I am only guessing with "daylight savings issue".

Hopefully anybody has a good idea.

John

Europe/London {color:#339966} *(correct calculation!)*{color}
daylight shift in millis: 3600000
Is in daylight savings: true
19.04.2010 11:28:53

*Europe/Berlin {color:#339966}(correct calculation){color}*
daylight shift in millis: 3600000
Is in daylight savings: true
19.04.2010 12:28:53

Australia/Sydney{color:#ff0000} (wrong calculation, shoul 1 hour){color}
daylight shift in millis: 3600000
Is in daylight savings: false
*19.04.2010 20:28:53*

America/New_York {color:#339966}(correct calculation){color}
daylight shift in millis: 3600000
Is in daylight savings: true
19.04.2010 06:28:53

Asia/Bangkok {color:#ff0000}(wrong calculation, shoud 1 hour){color}
daylight shift in millis: 0
Is in daylight savings: false
19.04.2010 17:28:53

Asia/Hong_Kong {color:#339966}(correct calculation){color}
daylight shift in millis: 0
Is in daylight savings: false
19.04.2010 18:28:53

``````package test.timezone;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.TimeZone;

public class TZCalc {

public static void main(String[] args) {

for (TimeZone tz : list) {
Calendar cal = new GregorianCalendar(tz);
SimpleDateFormat formatter = (SimpleDateFormat) DateFormat
.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
formatter.setTimeZone(tz);
System.out.println("\n"  +tz.getID());
System.out.println("daylight shift in millis: "  tz.getDSTSavings());
System.out.println("Is in daylight savings: "  tz.inDaylightTime(cal.getTime()));
System.out.println(formatter.format(cal.getTime()));
}
}

}``````
Edited by: jbegham on Apr 19, 2010 3:46 AM

Edited by: jbegham on Apr 19, 2010 3:47 AM
• ###### 1. Re: calculating time for different timezone, problems with daylight savings?
Currently Being Moderated
You should not set the time zone on the calendar since you want the calendar based on UTC.
``````        List<TimeZone> list = new LinkedList<TimeZone>();

Calendar cal = new GregorianCalendar(); // Regardless of your timezone this holds the number of milliseconds since 1/1/1970 UTC.

for (TimeZone tz : list)
{
SimpleDateFormat formatter = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
formatter.setTimeZone(tz);
System.out.println("\n" + tz.getID());
System.out.println("daylight shift in millis: " + tz.getDSTSavings());
System.out.println("Is in daylight savings: " + tz.inDaylightTime(cal.getTime()));
System.out.println(formatter.format(cal.getTime()));
} ``````
I have no idea whether or not this then gives the results you expect.
• ###### 2. Re: calculating time for different timezone, problems with daylight savings?
Currently Being Moderated
Hi,

thanks for the quick reply. I just checked it, but it seems not to have any impact as results stay exactly the same.

John
• ###### 3. Re: calculating time for different timezone, problems with daylight savings?
Currently Being Moderated
What version of Java are you using?
• ###### 4. Re: calculating time for different timezone, problems with daylight savings?
Currently Being Moderated
jbegham wrote:
Asia/Bangkok {color:#ff0000}(wrong calculation, shoud 1 hour){color}
daylight shift in millis: 0
Is in daylight savings: false
19.04.2010 17:28:53
Looks like you are wrong:
[http://www.timeanddate.com/worldclock/city.html?n=28]

UTC/GMT Offset
Standard time zone:     UTC/GMT +7 hours
No daylight saving time in 2010
• ###### 5. Re: calculating time for different timezone, problems with daylight savings?
Currently Being Moderated
jbegham wrote:
Australia/Sydney{color:#ff0000} (wrong calculation, shoul 1 hour){color}
daylight shift in millis: 3600000
Is in daylight savings: false
*19.04.2010 20:28:53*
That's also correct. I wonder why you didn't check your data.
• ###### 6. Re: calculating time for different timezone, problems with daylight savings?
Currently Being Moderated
Hey, not that harsh, please. You probably won't believe Kaj, but I did! [http://www.postsitter.de/auskunft/weltzeit.htm] .
And probably relying on that only source was wrong it seems. But only Sydney and Bangkok have different results on that site.

Well nevertheless thanks for helping.
• ###### 7. Re: calculating time for different timezone, problems with daylight savings?
Currently Being Moderated
jbegham wrote:
Hey, not that harsh, please. You probably won't believe Kaj, but I did! [http://www.postsitter.de/auskunft/weltzeit.htm] .
And probably relying on that only source was wrong it seems. But only Sydney and Bangkok have different results on that site.

Well nevertheless thanks for helping.
You have, hopefully, notified them that they have an error, right?