• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

SimpleDateFormat parses illegal date string  RSS feed

 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

I am having a SimpleDateFormat instance and passing an illegal date string to it's parse method, expecting an exception.

For some reason however, the pasing goes thru. The Date object is stale though.

Here is the code



Thanks

edit: is there any other way to check whether or not a date string conforms to a given format except for working with exceptions?
 
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not even thinking why it goes through but if you use format.setLenient(false);, you should get a ParseException.
 
Sheriff
Posts: 21465
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Call setLenient(false). By default, most DateFormat implementations are lenient. That means that they allow things like February 30th, or even a thirteenth month. It will just wrap these around - February 30th is March 2nd (or 1st in leap years), the thirteenth month will be January of the next year.
In your case it will use 2010 as the number of days in September 2017 (or 17, don't really know). It will overflow quite a few years I reckon

By turning leniency off you are prohibiting this behaviour. February 30th, a thirteenth month, or September 2010th 2017 will all cause errors.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome, it worked.

Any idea as to how to check for the validity of a date string other than working with exceptions like this:

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that dealing with the exception is the easiest way. The API already parses the string for you, why would you do it on your side too ?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Christophe Verré wrote:I think that dealing with the exception is the easiest way. The API already parses the string for you, why would you do it on your side too ?



I was looking for a method like 'isValidFormat' so I don't have to go thru the exceptions dealing.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's one :

but not in DateFormat
 
Rob Spoor
Sheriff
Posts: 21465
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, more people to tell about the other parse method!
DateFormat.parse(String) uses DateFormat.parse(String, ParsePosition) internally, and throws an exception if parsing fails:

As for the two returns from my code. The first return statement is the basic form; it will only check if the date String starts with a valid date. If the pattern is yyyy-MM-dd then it will consider 2010-09-17abc as valid. The second form also checks if the parsing consumes all of the String. With the given example, date.length() is 13 whereas parsing stops at 10. Since 10 != 13 it will return false.

This non-strict parsing allows you to combine multiple formatters with one string. The ParsePosition gets updated, so a next Format would start at position 10, ignoring the preceding text.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, hmm; interesting.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!