Search code examples
javalocalesimpledateformat

SimpleDateFormat and Locale API in Java


I have the following piece of code, which I thought would work as per what the documentation promised, but it didn't!

public static void main(String[] args) throws ParseException
{
    SimpleDateFormat fm = new SimpleDateFormat("yyyy/MMMM/dd",    // format str
                                               new Locale("gd",   // language = Scottish Gaelic
                                                          "UK",   // region   = UK
                                                          "scotland")); // variant = scotland
    fm.parse("2013/an Dàmhair/25");
}

Executing this (when it's put in a class with proper imports/declaration) will give the following error.

Exception in thread "main" java.text.ParseException: Unparseable date: "2013/an Dàmhair/25"
    at java.text.DateFormat.parse(DateFormat.java:357)
    at Foo.main(Foo.java:15)

Could anyone tell me if this is a bug (and/or unsupported feature)?

The date string is clearly valid date string in the Gaelic language, and the variant/language is also properly set. (Otherwise, I would expect to get IllegalArgument or some thing like that).

Any suggestion how to fix it would be greatly appreciated, too.

Thanks,


Solution

  • You can define your own DateFormatSymbols

    public static void main(String[] args) throws ParseException {
        DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(new Locale("gd", "UK", "scotland"));
        // sorry I don't know the other months in Scottish Gaelic. Thus the numbers
        dateFormatSymbols.setMonths(new String[] { "1", "2", "3", "4", "5",
                "6", "7", "8", "9", "an Dàmhair", "11", "12" }); 
    
        SimpleDateFormat fm = new SimpleDateFormat("yyyy/MMMMM/dd", dateFormatSymbols); 
        System.out.println( fm.parse("2013/an Dàmhair/25"));
    }
    

    I hope this is a solution for you