??? SimpleDateFormat Won't Provide Parsed Time Zone ???
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Tony_LaPaso
Posted On:   Sunday, June 9, 2002 10:13 PM

Hi all, The issue I'm facing is very simple to describe. First, I'm using J2SE 1.4.0_01 on Win 2k. I am running the program below in the U.S. Central Time Zone. The date I am parsing is, as you can see, specified in the the U.S. Eastern Time Zone. After the date is parsed, I need a way to determine what time zone the parsed date was specified in. I am parsing a date string ("Jul 13, 1997 14:52:16 EDT") using SimpleDateFormat (see below code). Unfortunately, the smart people at Sun decided to make the parse() method return a java.util.Date object instead of a java.util.Calendar object. Why is this bad? Because java.util.Date does not contain time zone information (which is part of the rea   More>>

Hi all,



The issue I'm facing is very simple to describe. First, I'm using J2SE 1.4.0_01 on Win 2k.



I am running the program below in the U.S. Central Time Zone. The date I am parsing is, as you can see, specified in the the U.S. Eastern Time Zone. After the date is parsed, I need a way to determine what time zone the parsed date was specified in.



I am parsing a date string ("Jul 13, 1997 14:52:16 EDT") using SimpleDateFormat (see below code). Unfortunately, the smart people at Sun decided to make the parse() method return a java.util.Date object instead of a java.util.Calendar object. Why is this bad? Because java.util.Date does not contain time zone information (which is part of the reason it was deprecated in the first place).



What's more, after parsing the date, the getTimeZone() of the SimpleDateFormat object does not return the time zone from the date that was just parsed, but instead returns the default time zone (i.e., where my JVM is running).



This just seems so dumb. Am I missing something? Is there a way for me to get at the time zone of the parsed date after the date is parsed? Yes, I know I could read the date string myself and then figure out the time zone and then map the value to the offset from GMT...but you know, the SimpleDateFormat class is ready doing this!



Sorry for the sarcastic tone -- I'm really getting fed up w/Sun.



Thanks!



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

public class DateTester {
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss zzz");

String s = "Jul 13, 1997 14:52:16 EDT";

System.out.println("Starting Time Zone: " + sdf.getTimeZone().getDisplayName());
Date date = sdf.parse(s);
System.out.println("The parsed date is: " + sdf.format(date));
System.out.println("Ending Time Zone: " + sdf.getTimeZone().getDisplayName());
} // main()
} // DateTester





Here's the output from running this program in the Central Time Zone:


			
Starting Time Zone: Central Standard Time
The parsed date is: Jul 13, 1997 13:52:16 CDT
Ending Time Zone: Central Standard Time
   <<Less

Re: ??? SimpleDateFormat Won't Provide Parsed Time Zone ???

Posted By:   Dave_Lorde  
Posted On:   Monday, June 10, 2002 04:18 PM

AIUI a date is just a date (i.e. a set number of milliseconds since the start-of-time in 1970), it doesn't have a time-zone because time-zones are locale dependent information. When you specify a time zone when parsing a date, this just tells the system how to calculate the UCT (Universal Co-ordinated Time) it represents. The default date display will be translated to the default time-zone.

For locale and time-zone conversions you use a Calendar. If you want to find out what the date is in another time-zone (e.g. the original one) you should use a Calendar and set the locale and time-zone appropriately.

Dave
About | Sitemap | Contact