Error parsing string to date in Java

Yes, another Java date post:)

My question:

Simple standard code to test whether the insertion date is in the form of request (DD / mm / yyyy):

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {

   sdf.parse(strDate); 

} catch(ParseException e) {

   // err

}

Question:

Who knows???

Solution

Just set setlenient to false

FROM API:

setLenient method:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
  sdf.setLenient(false);
  System.out.println(sdf.parse("2012/12/15")); 
} catch(ParseException e) {
  e.printStackTrace();
}
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
分享
二维码
< <上一篇
下一篇>>