This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Need Help on Time conversion. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need Help on Time conversion." Watch "Need Help on Time conversion." New topic
Author

Need Help on Time conversion.

Kate Xu
Greenhorn

Joined: Jul 28, 2003
Posts: 11
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
Marshal

Joined: Mar 22, 2005
Posts: 41065
    
  43

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 ]

Ping & DNS - my free Android networking tools app
Kate Xu
Greenhorn

Joined: Jul 28, 2003
Posts: 11
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

Joined: Sep 19, 2004
Posts: 531
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.
 
jQuery in Action, 2nd edition
 
subject: Need Help on Time conversion.
 
Similar Threads
Date printed in IST format when EDT format required
epoch is wrong
TimeZone Date convertion
GMT
Need an offset based on TimeZone given