Created
September 1, 2012 06:35
-
-
Save uttesh/3565739 to your computer and use it in GitHub Desktop.
TimeZoneConvertor with DST
This file contains hidden or 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("**************************************************"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment