Java – simpledateformat query

I have the following code snippet

Date date=null;
    SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
    try {
        date= sdf.parse("2001-02-2012");
        System.out.println(date);
    } catch (ParseException ex) {
        ex.printStackTrace();
    }

Strangely, it doesn't throw parsing exceptions, shouldn't it?

Solution

The problem is that your input is a little unexpected Then, simpledateformat tries to interpret it in a useful way So in your case, there are 2001 days, simply converted into weeks, months and years Your date is July 24, 2017 00:00:00 GMT. you can view on ideone: http://ideone.com/bfgogz

To "fix" it, you can add an SDF setLenient(false); Only accept inputs that are exactly the same as those you specified

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>