java日期格式化中ParsePosition问题

来源:互联网 发布:怎么投诉淘宝卖家骚扰 编辑:程序博客网 时间:2024/06/05 18:55

 

String data = "Fri Nov 28 00:00:00 CST 2008";

Date date = DateFormat.getDateInstance().parse(date);

会报错误ParseException

 

这是因为DateFormat中源码

    public Date parse(String source) throws ParseException
    {
        ParsePosition pos = new ParsePosition(0);
        Date result = parse(source, pos);
        if (pos.index == 0)
            throw new ParseException("Unparseable date: /"" + source + "/"" ,
                pos.errorIndex);
        return result;
    }

所以要使用下面的方式DateFormat.getDateInstance().parse(date,new ParsePosition(1))