Wondering what would be the best way to convert a server date/time to GMT timezone value taking into account the daylight savings time correction? Say, if I get a system date/time in the format of '01/01/2007 12:00:00 AM CST' like to convert it to '01/01/2007' 05:00:00 AM GMT considering there was a daylight savings correction done automatically.
I don't know if there is an API you can use. I can think of some way to solve this, but it'll be the hard way, parsing everything, calculating the.. nevermind!
What I would suggest is to move the question to another section: I don't think this is a beginner's question.
Probably you can do this with java.text.SimpleDateFormat and java.util.TimeZone. The TimeZone class already has all the rules about daylight saving built into it. Create one SDF to read the data, and use setTimeZone() to set it to whatever time zone the server is in. (Or if this code is running on the same server, then probably the default time zone is already correct.) Create another SDF to write the data, and set its time zone to GMT. Use the first SDF to read a Date, and use the second to write that Date as a new String.
The following code has been working for me to get the system date/time and return the value converted into GMT (as part of the requirement I had to return the date in String format) -
public static String DateNowAMPM() { // Get system date Calendar c = new GregorianCalendar(); c.setTime(new Date()); Date odate = c.getTime(); // Get TimeZone object for the system date TimeZone tzid = c.getTimeZone(); System.out.println("tzid : " + tzid); long inttimeinMS = odate.getTime();
// Converting to GMT int tzoffset = tzid.getRawOffset(); inttimeinMS = inttimeinMS - tzoffset; odate.setTime(inttimeinMS);
// Formatting the date. Adding AM/PM DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a"); System.out.println("formatter.format(odate) : <" + formatter.format(odate) + ">");
// Returning date in string format with am/pm return formatter.format(odate); }
You might find this simpler:Which, now that I reread the thread more closely, is exactly what Jim Yingst already said. [ March 05, 2007: Message edited by: Paul Clapham ]
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
Gift giving made easy with the permaculture playing cards