• Post Reply Bookmark Topic Watch Topic
  • New Topic

Strange date effect  RSS feed

 
Dave Mere
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Morning y'all.

I'm retrieving a string from a webform and processing it in a servlet. Testing has revealed a rather odd effect; if a supposedly invalid date such as "01/14/2005" is input the code accepts this as 01 Feb 2006. Equally "01/17/2005" is accepted as 01 May 2006.

The java seems to be just adding the any extra months after 12 into subsequent years. I fear this is not going to pass testing.

Here's the code:


Output of this is as stated above.

Anyone seen this effect before? And know how to get around it?

Thanks, DaveMere
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe try calling setLenient(false) on the DateFormat instance? I haven't tried this myself but its worth a shot.
 
Edwin Keeton
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are experiencing the joyful default behavior of a lenient Calendar class which does not validate against a strict range of "correct" values. When you get() a field in Calendar the input value is "normalized" such that the 14th month becomes the 2nd month of the following year, March 33 becomes April 2, etc.

If calling setLenient(false) on the formatter instance doesn't work, you may need to call setLenient(false) on the associated Calendar.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hehe..Calender - IIRC this is also the class where months are numbered 0-11 instead of 1-12.
 
Edwin Keeton
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gotta love it!
 
Jeffrey Spaulding
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stuart Gray:
Hehe..Calender - IIRC this is also the class where months are numbered 0-11 instead of 1-12.


Where is your point here

I'm working with Java for such a long time that i get the sum of 9 when i count my toes



J.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree - toes start counting at zero - everything knows this

I just remember using the Calendar class once and being surprised that months started with zero. I figured numbered months were 'standard' enough to have them start at one (as any non-technical person would probably assume too).
 
Dave Mere
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
setLenient(false) seems to work well.

Thanks very much to all for your responses!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!