On Tuesday 28 September 2004 8:02 am, Mario Ivankovits wrote:
> Steve Cohen wrote:
> >>>>form jui 7
> >>>>rather than
> >>>>7 jui
>
> Steven, we have a problem?

Yes indeed.  Thanks!  Ouch!
>
> I have tried to parse the date you shown in your ftp-locales test.
>
>         SimpleDateFormat sdf = new SimpleDateFormat("MMM dd", new
> Locale("fr", "FR"));
>         Date dt = sdf.parse("jui 7");
>
> "jui 7" is not parseable!!!!!!
> java.text.ParseException: Unparseable date: "jui 7"
>     at java.text.DateFormat.parse(DateFormat.java:335)
>
> while "juil."  (javas short french form) is.
>
>         SimpleDateFormat sdf = new SimpleDateFormat("MMM dd", new
> Locale("fr", "FR"));
>         Date dt = sdf.parse("juil. 7");

This is what I get for making assumptions in this field (and a strong 
cautionary note to those who might still want to try for an automated 
auto-detect system.

Noting the strong similarities between "ls" listings and listings from ftp 
servers, I assumed that what I see in a unix directory listing created with a 
specific unix "LANG" would be the same as what we would see in java, created 
with a specific "Locale".  Because American unix directory and ftp listings 
use the same month abbreviations as those returned by 
SimpleDateFormat.getDateFormatSymbols().getShortMonths(), I erroneously 
assumed that this must be the case for all LANGs and their equivalent 
Locales.  

As you so cogently point out, that's not the case!  Doh!

Which means, back to the drawing board!  Java and its SimpleDateFormats are 
not going to help us as much.  There is no simple path from a Locale to a 
directory listing date format.

So I see two possiblities.
1) Parse the date with a regular expression but make the month names a 
settable parameter.
2) Parse the date with a special SimpleDateFormat constructed on the fly:

private SimpleDateFormat createDateFormatter(
        String formatString,  /* e.g "MMM dd"*/
        String monthNames) /* e.g 
"jan|fÃv|mar|avr|mai|jun|jui|aoÃ|sep|oct|nov|dÃc"*/
{
        sdf = new SimpleDateFormat(formatString); 
        sdf.getDateFormatSymbols().setShortMonthNames(monthNames.split("|");  
/*
yes,I know that String.split() is java 1.4 specific, this is just for 
simplicity here.  Any actual implementation could not use the split() method.
*/
}

The advantage of 2 is that you still get a Date object after all your pains, 
more easily that you do from rolling your own off a regex.  And it's easier 
to use SimpleDateFormat format strings than regular expressions.  Finally, 
there is more calendar logic in SimpleDateFormat than in our reqular 
expressions.  Please note that using our regexes Feb 30 is a legitimate date 
in our regex system.

In either case, for the sake of user convenience we might still want to tie 
some preset constant month  to locales (and possibly system types), even 
though java's implementation does not produce the same symbols natively as 
unix directory listings do.


>
> ---
> Mario


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to