Parsing IETF standard date format.

A

Assafsuf

In the depreceated Date.parse() method javadoc it says:

It accepts many syntaxes; in particular, it recognizes the IETF
standard date syntax: "Sat, 12 Aug 1995 13:30:00 GMT".

Now, I am trying to use the DateFormat class to parse a string in that format
with no luck.
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);

Thanks
Assafsuf
 
T

Thomas Fritsch

Assafsuf said:
...
Now, I am trying to use the DateFormat class to parse a string in that format
with no luck.
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);
I succeeded with the following:
String date = "Sat, 12 Aug 1995 13:30:00 GMT";
SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy
HH:mm:ss z", Locale.ENGLISH);
Date d = formatter.parse(date);
System.out.println(d);

Thomas
 
P

P.Hill

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
 
A

Assafsuf

Thomas Fritsch said:
I succeeded with the following:
String date = "Sat, 12 Aug 1995 13:30:00 GMT";
SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy
HH:mm:ss z", Locale.ENGLISH);
Date d = formatter.parse(date);
System.out.println(d);

Thomas


This works for me. Thanks a lot. Weird though, what made the difference is
the Locale addition...
 
A

Assafsuf

P.Hill said:
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

Thanks,
The problem was that I was getting a parse exception if I didn't
specify Locale.english.
Assaf.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top