java.text.ParseException及日期格式化

在优化接口时,遇到该异常,导致前端页面一直报500错误:

 Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.text.ParseException: Unparseable date: "Fri Apr 07 20:16:19 CST 2023"] with root cause
java.text.ParseException: Unparseable date: "Fri Apr 07 20:16:19 CST 2023".....

经过一段时间搜索和查找资料,发现是由于使用了以下的格式转化方式导致该异常:

Format dataFormat = new SimpleDateFormat("yyyy-MM-dd");
Date parse = dataFormat.parse(String.valueOf(date));
System.out.println(parse);

我的初衷是想让时间格式话,所以采用了该方法,然而这样的作法会导致该异常,导致前端接受数据发生中断。于是我打开parseObject的源码:

public abstract Object parseObject (String source, ParsePosition pos);

    /**
     * Parses text from the beginning of the given string to produce an object.
     * The method may not use the entire text of the given string.
     *
     * @param source A <code>String</code> whose beginning should be parsed.
     * @return An <code>Object</code> parsed from the string.
     * @exception ParseException if the beginning of the specified string
     *            cannot be parsed.
     */
 public Object parseObject(String source) throws ParseException {
        ParsePosition pos = new ParsePosition(0);
        Object result = parseObject(source, pos);
        if (pos.index == 0) {
            throw new ParseException("Format.parseObject(String) failed",
                pos.errorIndex);
        }
        return result;
    }

从该源码可以看出,我们必须要有 ParsePosition参数,就是第一个parseObject(该参数使用了@NotNUll注解,这里显示不出来),有源码知该异常的主要是由于pos.index == 0。然而我们由于没有传入该参数导致使用了第二个parseObject方法,而该方法直接设置index=0,所以报异常,于是我又打开ParsePosition的源码:

public class ParsePosition {

    /**
     * Input: the place you start parsing.
     * <br>Output: position where the parse stopped.
     * This is designed to be used serially,
     * with each call setting index up for the next one.
     */
    int index = 0;
    int errorIndex = -1;

    /**
     * Retrieve the current parse position.  On input to a parse method, this
     * is the index of the character at which parsing will begin; on output, it
     * is the index of the character following the last character parsed.
     *
     * @return the current parse position
     */
    public int getIndex() {
        return index;
    }

    /**
     * Set the current parse position.
     *
     * @param index the current parse position
     */
    public void setIndex(int index) {
        this.index = index;
    }

    /**
     * Create a new ParsePosition with the given initial index.
     *
     * @param index initial index
     */
    public ParsePosition(int index) {
        this.index = index;
    }
}

可以看出默认index=0,这就是如果我们即使传入该参数,但index设置为0,或者-1,都会导致错误。

后面我加上该参数后测试成功了。

由这个异常我进行了其他的格式化测试:

测试一:

    @Test
    public void formatTest() throws ParseException {
        Date date = new Date();
        System.out.println(date);
    }
}

输出结果:

测试二:

 @Test
    public void formatTest() throws ParseException {
           DateFormat df2 = DateFormat.getDateInstance(DateFormat.MEDIUM);
           System.out.println(df2.format(new Date()));
    }

 输出结果:

 这是由于 DateFormat类中有很多内置的数据格式,这里使用的是MEDIUM,默认的数据格式。

 如果不想使用内置的数据格式,我们需要使用String类提供的format:

@Test
    public void formatTest() throws ParseException {
        Date date = new Date();
        System.out.println(String.format("%tc",date));
        System.out.println(String.format("%tF",date));
        System.out.println(String.format("%tD",date));
        System.out.println(String.format("%tr",date));
        System.out.println(String.format("%tT",date));
        System.out.println(String.format("%tR",date));
    }

输出:

这里是该方法的转化规则:

 如有错误,还望指正!

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值