Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

calculating time for different timezone, problems with daylight savings?

807580Apr 19 2010 — edited Apr 20 2010
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.

Thanks in advance
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.LinkedList;
import java.util.List;
import java.util.TimeZone;

public class TZCalc {

public static void main(String[] args) {

List<TimeZone> list = new LinkedList<TimeZone>();
list.add(TimeZone.getTimeZone("Europe/London"));
list.add(TimeZone.getTimeZone("Europe/Berlin"));
list.add(TimeZone.getTimeZone("Australia/Sydney"));
list.add(TimeZone.getTimeZone("America/New_York"));
list.add(TimeZone.getTimeZone("Asia/Bangkok"));
list.add(TimeZone.getTimeZone("Asia/Hong_Kong"));

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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 18 2010
Added on Apr 19 2010
7 comments
440 views