Re: Parsing IETF standard date format.

From: Assafsuf (assaf_at_il.ibm.com)
Date: 12/14/03


Date: 14 Dec 2003 06:45:34 -0800


"P.Hill" <goodhill@xmission.com> wrote in message news:<bra5op$par$1@terabinaries.xmission.com>...
> Assafsuf wrote:
> > 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.



Relevant Pages

  • RE: SaveWorkbookAsToday()
    ... Create a string variables for the day and convert your date to the format you ... Then append or concatenate the string date where necessary in the file name. ... Dim DateFormat As String ...
    (microsoft.public.excel.programming)
  • Re: Parsing IETF standard date format.
    ... I am trying to use the DateFormat class to parse a string in that format ... UNTIL YOU CONVERT IT TO A STRING. ... If all of the Calendar functionality was in Date You'd have to have a way to set ... That is the intent of DateFormat and Calendars. ...
    (comp.lang.java.programmer)
  • Re: International date format and user form
    ... Here is a past function I originally developed for a question over in the compiled VB newsgroups which will format a date in the same format that Windows is using... ... Function DateFormatAs String ... DateFormat = Replace, "MMM") ... Dim DateSep As String ...
    (microsoft.public.excel.programming)
  • Re: International date format and user form
    ... Function DateFormatAs String ... DateFormat = Replace) ... compiled VB newsgroups which will format a date in the same format that ... Dim DateSep As String ...
    (microsoft.public.excel.programming)
  • Re: Retrieving short system date format in VBA
    ... Thanks Rick, this is excaclty what I'm looking for. ... Function DateFormat() As String ... you can get the time format pattern using this function... ...
    (microsoft.public.excel.programming)