wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date/Time conversion to GMT with DayLightSavings" Watch "Date/Time conversion to GMT with DayLightSavings" New topic
Author

Date/Time conversion to GMT with DayLightSavings

Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
Hello,

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.

Thanks.

Tariq
Eduardo Ellery
Greenhorn

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.

BR,
Eduardo Ellery
Jim Yingst
Wanderer
Sheriff

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:

http://joda-time.sourceforge.net/
http://joda-time.sourceforge.net/quickstart.html

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

Joined: Oct 14, 2005
Posts: 18541
    
    8

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 ]
 
jQuery in Action, 2nd edition
 
subject: Date/Time conversion to GMT with DayLightSavings
 
Similar Threads
epoch is wrong
Daylight saving issue.
Converting a date object from GMT to EST
Time zone issue
Need Help on Time conversion.