简体   繁体   中英

SimpleDateFormat.parse()

SimpleDateFormat.parse() accepts the date 003/1/2011 when the format is MM/dd/yyyy . Trying with code below:

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
Date dt2;
try
{
    dt2 = sdf.parse(_datemmddyyyy);
}
catch (ParseException e)
{
    return false;
}

and the date is parsed as 00/11/2011 . What is wrong?

Are you sure? This:

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
Date dt2 = sdf.parse("003/1/2011");
System.out.println(dt2);

Yields:

Tue Mar 01 00:00:00 PST 2011

That's 03/01/2001 in MM/dd/yyyy . Seems right?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM