• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date/Time conversion to GMT with DayLightSavings

 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Niall Loughnane
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20736
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic