【问题标题】:SimpleDateFormat and Locale API in JavaJava 中的 SimpleDateFormat 和 Locale API
【发布时间】:2013-10-28 14:31:59
【问题描述】:

我有以下代码,我认为它可以按照文档承诺的那样工作,但事实并非如此!

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 或类似的东西)。

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

谢谢,

【问题讨论】:

  • Locale 是否真的知道您使用的月份名称?如果您使用日期格式化程序格式化当前日期会发生什么?
  • 啊,好问题。我会检查。谢谢
  • 好的,它给了我一个简单的英文字符串。但这是错误。英语和苏格兰盖尔语相去甚远!
  • 好的,那么您有任何迹象表明苏格兰盖尔语支持作为一种语言?它存在于Locale.getAvailableLocales 中吗?
  • 苏格兰盖尔语不是 Java 支持的语言环境。 AFAIK,你不会得到任何异常,因为 Java SE 中没有这样的异常

标签: java locale simpledateformat


【解决方案1】:

根据this list,不支持此语言环境。

【讨论】:

【解决方案2】:

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

我希望这对你来说是一个解决方案

【讨论】:

  • 啊,有趣。我想这对我有用。谢谢!
猜你喜欢
  • 2017-06-09
  • 1970-01-01
  • 1970-01-01
  • 2011-09-18
  • 2018-09-15
  • 1970-01-01
  • 1970-01-01
  • 2014-05-17
  • 2012-10-12
相关资源
最近更新 更多