• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need Help on Time conversion.

 
Kate Xu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am in U.S. Eastern Standard Time Zone (4 hours behind GMT during daylight savings time, or 5 hours behind GMT during eastern standard time).

My web application is running on Weblogic server, the server is using GMT system time zone.

I need to convert input from UI, which is in EST String format ( "9/1/2005 00:00"), to GMT Timestamp object (9/1/2005 04:00).

How can I do it? I tried this and it did not work:

import java.util.*;
import java.text.SimpleDateFormat;

public class TZ1 {
public static void main(String[] args) {

TimeZone tz = TimeZone.getTimeZone("US/Eastern");
TimeZone GMT_TIME_ZONE = TimeZone.getTimeZone("GMT");

Calendar estCalendar = Calendar.getInstance(tz);
Calendar gmtCalendar = Calendar.getInstance(GMT_TIME_ZONE);

String dateString = "9/1/2005";
String formatString = "MM/dd/yyyy";

SimpleDateFormat eptDateFormat = new SimpleDateFormat(formatString);
eptDateFormat.setTimeZone(tz);

SimpleDateFormat gmtDateFormat = new SimpleDateFormat(formatString);
gmtDateFormat.setTimeZone(GMT_TIME_ZONE);

try {
System.out.println("dateString is: " + dateString);

//Date resultDate1 = eptDateFormat.parse(dateString);
//System.out.println("resultDate1 is: " + resultDate1);

Date resultDate2 = gmtDateFormat.parse(dateString);
System.out.println("resultDate2 is: " + resultDate2);

} catch (Exception e) {

}

}
}
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I tried this and it did not work:


What does this mean? What output did you get?


} catch (Exception e) {

}


It's almost never a good idea to ignore exceptions you're catching without at least generating a message that an exception was thrown - especially when working on a program that does not (yet) work correctly.
[ August 22, 2005: Message edited by: Ulf Dittmer ]
 
Kate Xu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got an output, but it was not what I expected:

dateString is: 9/1/2005
resultDate2 is: Wed Aug 31 20:00:00 EDT 2005

I expected resultDate2 to be: Thu Sep 1 04:00:00 GMT 2005
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a lot going on there but let's get to the core issue. Regardless of what value you have in resultDate2, if you are getting EDT then the system's using EDT not GMT.

>> System.out.println("resultDate2 is: " + resultDate2);

The key here is that Date values are effectively absolute. They never carry timezone or formatting information. Try a simple one-line program that prints the date.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic