java - SimpleDateFormat.parse() - generates wrong date for different date-formats -


below code parse date using simpledateformat pattern:

string pattern = "yyyy-mm-dd";     simpledateformat format = new simpledateformat(pattern); try {     date date = format.parse("05-21-2030");     system.out.println(date); } catch (parseexception e) {     e.printstacktrace(); } 

you can see date passed parse different date format specified in simpledateformat. in case expecting kind of excpetion format different parsed different date values. got output - tue mar 22 00:00:00 ist 12

when pass same format 2030-05-21 works fine.

can guys please let me know how can prevent such things in code?

basically want simpledateformat strict, set lenient false.

simpledateformat format = new simpledateformat(pattern); format.setlenient(false); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -