aspose file tools
The moose likes Java in General and the fly likes java.text.SimpleDateFormat does not behaves as expected Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "java.text.SimpleDateFormat does not behaves as expected " Watch "java.text.SimpleDateFormat does not behaves as expected " New topic
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
    
    3

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.text.SimpleDateFormat does not behaves as expected
 
Similar Threads
No exception when parsing date from string
Date Format Issue
Date Validation
Max Date Limit
Why is 01/01/2000 unparsable?