While converting the timezone we have to consider DST standard also. in jdk 1.3. and 1.4 DST was not taken care internally by jdk, But from jdk 1.5 it is taken care.
What is daylight saving or DST?
Daylight Saving Time (or summertime as it is called in many countries) is a way of getting more light out of the day by advancing clocks by one hour during the summer. During Daylight Saving Time, the sun appears to rise one hour later in the morning, when people are usually asleep anyway, and sets one hour later in the evening, seeming to stretch the day longer.
About Daylight Saving Time.
for brief history on daylight visit the below link
http://en.wikipedia.org/wiki/Daylight_saving_time
In jDK we have a mehod inDaylightTime() in TimeZone class to check is given date is in DST for the timezone
ex : timeZone.inDaylightTime(new Date());
sample code :
public static boolean IsTimeZoneInDST(TimeZone timeZone) throws ParseException{
return timeZone.inDaylightTime(new Date());
}
now we have to convert the time from one timezone to another by using the timezone rawOffset.
sample :
public static Timestamp convert(Timestamp timeStamp, TimeZone fromTimeZone, TimeZone toTimeZone) {
// if null, convert from UTC
long offFrom = fromTimeZone == null ? 0 : fromTimeZone.getOffset(timeStamp.getTime());
// if null, convert to UTC
long offTo = toTimeZone == null ? 0 : toTimeZone.getOffset(timeStamp.getTime());
return new Timestamp(timeStamp.getTime()/1000*1000 + timeStamp.getNanos()/1000000 + offTo - offFrom);
}
now in above we have some thing fishy i.e
timeStamp.getTime()/1000*1000 + timeStamp.getNanos()/1000000
the above code was for the jdk 1.3 and 1.4, Because its was a bug in the jdk see the below link
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4679060
sample TimeZoneConverter.java class download
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Timestamp; | |
import java.util.TimeZone; | |
import java.util.logging.Logger; | |
import java.util.*; | |
public class TimeZoneConvertor { | |
private static final TimeZone defaultTimeZone = TimeZone.getDefault(); | |
private static final Logger log = Logger.getLogger("TimeZone Convertor"); | |
/** | |
* Convert input time stamp to UTC/GMT | |
* @param timestamp | |
* @return | |
*/ | |
public static Timestamp convertToUTC(Timestamp timestamp){ | |
return convert(timestamp,defaultTimeZone,null); | |
} | |
/** | |
* Convert from UTC/GMT | |
* @param timestamp | |
* @return | |
*/ | |
public static Timestamp convertFromUTC(Timestamp timestamp){ | |
return convert(timestamp,null,defaultTimeZone); | |
} | |
/** | |
* Convert the Timestamp From one timezone to another | |
* | |
* @param timestamp | |
* @param fromTimeZone | |
* @param toTimeZone | |
* @return | |
*/ | |
private static Timestamp convert(Timestamp timestamp,TimeZone fromTimeZone, TimeZone toTimeZone) { | |
long offFrom = fromTimeZone == null ? 0 : fromTimeZone.getOffset(timestamp.getTime()); | |
long offTo = toTimeZone == null ? 0 : toTimeZone.getOffset(timestamp.getTime()); | |
return new Timestamp(timestamp.getTime()/1000*1000 + timestamp.getNanos()/1000000 + offTo - offFrom); | |
} | |
public static void DisplayOffset(String text,int rawOffset,TimeZone timeZone){ | |
int hour = rawOffset/(60*60*1000); | |
int minutes = Math.abs(rawOffset/(60*1000)) % 60; | |
System.out.println(text+timeZone.getDisplayName()+ " "+hour+ ":"+minutes+" hours"); | |
} | |
public static void main(String[] args) { | |
Date date = new Date(); | |
Timestamp timestamp = new Timestamp(date.getTime()); | |
// To Get Available TimeZones | |
/*String []TimezoneIDs = TimeZone.getAvailableIDs(); | |
for(String Id : TimezoneIDs) | |
System.out.println("Timzone ID : \t"+Id);*/ | |
TimeZone fromTimeZone = TimeZone.getTimeZone("Asia/Kolkata"); | |
TimeZone toTimeZone = TimeZone.getTimeZone("America/Chicago"); | |
/* | |
* Now we have to convert from current timezone to target time | |
* | |
* First we have to convert the fromTimezone Time to GMT i.e difference with GMT | |
* | |
* Get the converted timestamp i.e in GMT time now | |
* | |
* now convert timestamp from GMT to target time zone | |
*/ | |
System.out.println("******************** Off set from GMT ************"); | |
DisplayOffset("From : ", fromTimeZone.getRawOffset(), fromTimeZone); | |
DisplayOffset("To : ", toTimeZone.getRawOffset(), toTimeZone); | |
System.out.println("Current Time \t"+timestamp); | |
timestamp = convert(timestamp, fromTimeZone, TimeZone.getTimeZone("GMT")); | |
System.out.println("Timestamp to GMT\t"+timestamp); | |
timestamp = convert(timestamp, TimeZone.getTimeZone("GMT"), toTimeZone); | |
System.out.println("GMT to target Timezone \t"+timestamp); | |
System.out.println("**************************************************"); | |
} | |
} |
0 comments:
Post a Comment