wood burning stoves
The moose likes Beginning Java and the fly likes Date/Time conversion to GMT with DayLightSavings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date/Time conversion to GMT with DayLightSavings" Watch "Date/Time conversion to GMT with DayLightSavings" New topic

Date/Time conversion to GMT with DayLightSavings

Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116

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.


Eduardo Ellery

Joined: Feb 28, 2007
Posts: 2
Hi Tariq!

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.

Eduardo Ellery
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
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.

"I'm not back." - Bill Harding, Twister
Niall Loughnane
Ranch Hand

Joined: Dec 07, 2006
Posts: 208
check out joda:


it is good for date time conversions
Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
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;

// 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);
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

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 ]
I agree. Here's the link: http://aspose.com/file-tools
subject: Date/Time conversion to GMT with DayLightSavings
It's not a secret anymore!