Welcome to Dream.In.Code
Become a Java Expert!

Join 149,617 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,862 people online right now. Registration is fast and FREE... Join Now!




SimpleDateFormat > Me

 
Reply to this topicStart new topic

SimpleDateFormat > Me, If it only knew exactly what I was thinking...

majorpayne27
21 Aug, 2007 - 10:12 AM
Post #1

New D.I.C Head
*

Joined: 10 Jul, 2007
Posts: 5


My Contributions
I apologize for this noob-level question, but I'm angry at SimpleDateFormat and am wondering in anyone knows how to make it do what I want.

My issue is that I'm using SimpleDateFormat to basically validate different types of dates. This usually works fine, except with the terminating field of a date. For example, I parse the String "08/21/2007" against the format "MM/dd/yyyy" and confirm the date is valid.

My problem occurs when the user enters "bad" data for the year. If non-valid character are put in the month or date fields, they are handled with an error perfectly fine. However, if the user enters a letter somewhere in the year (say they use the letter O instead of a zero) SimpleDateFormat just views it as a terminating character. In short, the string "08/27/2OO7" (with letter O's) is parsed to "08/27/2" when it should be throwing an error. Is there any way to force SimpleDateFormat's parse to check the entire string?
User is offlineProfile CardPM
+Quote Post

Programmist
RE: SimpleDateFormat > Me
21 Aug, 2007 - 03:51 PM
Post #2

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
I haven't been able to find a way to change the behavior of the parse method to throw an exception in this scenario. There is a setLenient(boolean B) method, but it does not solve the problem. My advice to you is to validate the date you get to see if it is too long ago. If you don't have a "valid" date range then you can test the date portion of the string with Integer.parseInt, but that kind of defeats the purpose of letting SimpleDateFormat parse it for you.

This post has been edited by skyhawk133: 21 Aug, 2007 - 03:57 PM
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: SimpleDateFormat > Me
21 Aug, 2007 - 04:00 PM
Post #3

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
the date formats in java are messed up and complicated to convert and compare. Hopefully it will be addressed in an upcoming update.
alcdotcom is right, it's about your best choice.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:36AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month