Assafsuf said:
Now, I am trying to use the DateFormat class to parse a string in that format
with no luck.
In the future, it would be better if you would define more cleary the term
"luck". Give an example of what you think is wrong. But in this case, I
think I know.
Can anyone post a sample code for that, or show me what is wrong in
the code below?
SimpleDateFormat formatter = (SimpleDateFormat)DateFormat.getDateInstance();
formatter.applyPattern("EEE, d MMM yyyy HH:mm:ss z");
Date d = formatter.parse(date);
Nothing is wrong with the code, but I'm sure you then used d.toString()
to look at the value in d. Dates are binary values. They have no idea
of Timezones, UNTIL YOU CONVERT IT TO A STRING. at that point it has to
pick a timezone. In that case it just uses the VM default.
You probably were thinking that a Date internally new about hours, minutes
and especially a TZ. It is a binary value, that is all.
In the following example, my current default is MST (North American/Denver)
Try this code:
public class ParseDate {
public static void main(String[] args) throws ParseException {
String dateStr = "Thu, 11 Dec 2003 22:13:14 MST";
SimpleDateFormat formatter =
(SimpleDateFormat)DateFormat.getDateInstance();
formatter.applyPattern("EEE, d MMM yyyy HH:mm:ss z");
Date date = formatter.parse(dateStr);
System.out.println( date.getTime() + ": date.toString() = " + date +
" : the original string is: " + dateStr );
dateStr = "Sat, 12 Aug 1995 13:30:00 GMT";
date = formatter.parse( dateStr );
System.out.println(
"The following only differ by the current default timezone.");
System.out.println( date.getTime() + ": date.toString() = " + date +
" : the original string is: " + dateStr );
formatter.getCalendar().setTimeZone( TimeZone.getTimeZone( "GMT" ) );
date = formatter.parse( dateStr );
System.out.println(
"After tweeking the timezone in the calendar of the date format then
parsing and formatting with that.");
System.out.println( date.getTime() + ": formated " +
formatter.format(date) + " : the original string is: " + dateStr );
System.out.println(
"Notice that the last dates have the same internal binary values.");
}
}
Which results in:
1071205994000: date.toString() = Thu Dec 11 22:13:14 MST 2003 : the original
string is: Thu, 11 Dec 2003 22:13:14 MST
The following only differ by the current default timezone.
808234200000: date.toString() = Sat Aug 12 07:30:00 MDT 1995 : the original
string is: Sat, 12 Aug 1995 13:30:00 GMT
After tweeking the timezone in the calendar of the date format then parsing and
formatting with that.
808234200000: formated Sat, 12 Aug 1995 13:30:00 GMT : the original string is:
Sat, 12 Aug 1995 13:30:00 GMT
Notice that the last dates have the same internal binary values.
Now you can begin to understand why Date.parse() is deprecated.
If all of the Calendar functionality was in Date You'd have to have a way to set
a Date into a mode where it would be working in a different TZ. All such
modes would result in a lot of static methods like Date.setToStringTimeZone().
That is the intent of DateFormat and Calendars. To hold a binary date
use Date. To break a date into separate fields use a Calendar. To go directly
to a formatted String us DateFormat.
HTH,
-Paul