【问题标题】:String to Date Parse Exception with FastDateFormat使用 FastDateFormat 的字符串到日期解析异常
【发布时间】:2017-11-21 19:19:33
【问题描述】:

我想通过FastDateFormatString 解析为Date 对象。

simpleDateStr"04/13/2017" 这是我的代码:

private static final String SIMPLE_DATE_FORMAT = "MM/dd/yyyy";

(Date) FastDateFormat.getInstance(SIMPLE_DATE_FORMAT).parseObject(simpleDateStr);

我得到了那个例外:

java.text.ParseException: Format.parseObject(String) failed
    at java.text.Format.parseObject(Format.java:245)

依赖:

<dependency>
  <groupId>commons-lang</groupId>
  <artifactId>commons-lang</artifactId>
  <version>2.6</version>
</dependency>

【问题讨论】:

  • 尝试使用parse(simpleDateStr)而不是parseObject(simpleDateStr),那么您也不需要将结果转换为Date类型。
  • 对我来说很好用(parseparseObject),所以我想是时候创建一个minimal reproducible example了。确保simpleDateStr 真的“04/13/2017”并且不包含任何隐藏字节。
  • 好的。我找到了。我刚刚意识到我使用的是 2.6。它有一个错误,切换到 3.6 修复了它。

标签: java date type-conversion


【解决方案1】:

似乎 2.6 有一个错误,切换到 3.6 解决了这个问题。

PS:原因是它没有像@Shekhar Khairnar 回答的那样实现。

【讨论】:

    【解决方案2】:

    在 2.6 中的实现(直接来自 FastDateFormat 的源代码)是这样的:

    // Parsing
        //-----------------------------------------------------------------------
        /**
         * <p>Parsing is not supported.</p>
         * 
         * @param source  the string to parse
         * @param pos  the parsing position
         * @return <code>null</code> as not supported
         */
        public Object parseObject(String source, ParsePosition pos) {
            pos.setIndex(0);
            pos.setErrorIndex(0);
            return null;
        }
    

    这是罪魁祸首

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-06
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多