| Author |
java.text.SimpleDateFormat does not behaves as expected
|
ajinkyad desai
Greenhorn
Joined: Aug 13, 2007
Posts: 1
|
|
Hi, See the following code SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); sdf.setLenient(false); Date st = sdf.parse("04-12-2000sdfsdfsf"); System.out.println(st); It will produce output as Mon Dec 04 00:00:00 IST 2000 instead of throwing ParseException and also if I give SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); sdf.setLenient(false); Date st = sdf.parse("04-12-200046649sdfsdfsf"); System.out.println(st); It will produce output as Tue Dec 04 00:00:00 IST 200046649 It ignores trailing non-numeric characters of last field.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12950
|
|
Even when you call setLenient(false) on the SimpleDateFormat object, it isn't really strict with checking the format of the input string. Unfortunately the API documentation doesn't exactly specify the difference between setting lenient to true or false. If you want to check if the input string matches a certain format exactly, you could use a regular expression to check it before you parse it.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: java.text.SimpleDateFormat does not behaves as expected
|
|
|