【问题标题】:SimpleDateFormat Unparseable date: "9-Jul" when using parse methodSimpleDateFormat 无法解析的日期:使用解析方法时的“9-Jul”
【发布时间】:2014-03-08 07:42:15
【问题描述】:

我想用SimpleDateFormat.parse方法来设置日期:

 private static SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM");
 ...
  Calendar calendar = Calendar.getInstance();
  int year = calendar.get(Calendar.YEAR);
  calendar.setTime(fmt.parse("9-Jul"));
  calendar.set(Calendar.YEAR, year);

但我明白了:

Exception in thread "main" java.text.ParseException: Unparseable date: "9-Jul"
    at java.text.DateFormat.parse(Unknown Source)
    at BusinessPlan.main(BusinessPlan.java:116)

代码来自 Apache 示例站点,并以this bug 发布。

【问题讨论】:

    标签: java date datetime apache-poi simpledateformat


    【解决方案1】:

    适合我

    尝试设置Locale

     SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM", Locale.US);
    

    【讨论】:

    • 如何设置语言环境以及我应该在哪里设置。
    • 抱歉在之前的编辑中错过了,请在回答中查看更新
    【解决方案2】:

    我认为因为您使用“dd”,所以您必须使用 09 而不是 9。

    【讨论】:

    • 好点,但没有帮助。可能是本地化问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 2017-02-28
    • 1970-01-01
    • 2023-03-20
    • 2011-09-12
    • 1970-01-01
    相关资源
    最近更新 更多