我有以下代码,我认为可以按照文档中所承诺的那样工作,但是没有!

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");
}


执行此操作(将其放入具有正确导入/声明的类中时)将产生以下错误。

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)


谁能告诉我这是一个错误(和/或不受支持的功能)吗?

日期字符串显然是盖尔语中的有效日期字符串,并且变体/语言也已正确设置。 (否则,我希望得到IllegalArgument或类似的东西)。

任何建议如何解决它也将不胜感激。

谢谢,

最佳答案

您可以定义自己的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"));
}


我希望这是您的解决方案

10-06 05:34
查看更多