| Author |
Suggestions for date validation
|
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Guys,
I know that this must have been implemented a million times, but I'm here looking for ideas:
I need to validate the date for a specific format. I have both the inputs (date format and the actual date) as String objects. I can use a variety of API's to do this (Calendar, SimpleDateFormat, DateUtils etc., ) but I would like to have suggestions on the simplest one.
For example: isDateValid("DDMMYYYY", "21122010")
In all my date formats, I do not have any seperators (like . or -). It is always either DDMM or DDMMYYYY or any of that combination but always without a seperator.
|
SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
|
Suggestions???
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
Remember that PatienceIsAVirtue!
I would use SimpleDateFormat.
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
And remember that the pattern characters are case sensitive. YYYY is invalid, and DD returns the day of the year, not the day of the month. You want "ddMMyyyy" or "ddMM". Because yes, MM should remain uppercase as mm is reserved for minutes.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
It looks like the SimpleDateFormat would not be suitable for me:
It looks like only the date format is validated but not the date, month or year.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Sure it is, with a bit of tweaking:
|
 |
Sean Clark
Rancher
Joined: Jul 15, 2009
Posts: 377
|
|
Rob, your post you posted has a minor bug, should be: rather than.
Nice bit of of code though, I didn't know about this
Sean
|
I love this place!
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Nope it still prints the wrong values:
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
|
It was a mistake from my side. It worked perfectly fine. Thanks!
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Sean Clark wrote:Rob, your post you posted has a minor bug, should be: rather than .
Nice bit of of code though, I didn't know about this
Sean
You're very right of course.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Joe Harry wrote:It was a mistake from my side. It worked perfectly fine. Thanks!
You're welcome
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
|
I have another question in this context. I have the date as MMyy and from this I need to get the date. Any suggestions on how this could be acheived?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Parse to Date, then convert to Calendar, then retrieve the information you need:
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
|
Another hint to my requirement is that I need the date always to be the last date of that month and year. I hope that there should be methods like getLastDate() or similar??
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Calendar has a method called getActualMaximum; it uses its current date/time to determine the result. So getActualMaximum(Calendar.DAY_OF_MONTH) would return 28 if the calendar's date was in February, and 30 for this month.
Don't confuse this method with getMaximum which will return the absolute maximum; getMaximum(Calendar.DAY_OF_MONTH) would return 31, whatever the calendar's date would be.
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Thanks for the suggestions. Would try that you!
|
 |
Gaurav Kr. Arora
Ranch Hand
Joined: Feb 20, 2011
Posts: 37
|
|
Joe Harry wrote:Nope it still prints the wrong values:
Could you please explain why position.getIndex() == date.length(); condition is needed in the return statement above? Can't we just use d!=null?
[size=9]PS: Am new user of SimpleDateFormat and might not be fully aware of its functionality.[/size]
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Try using "01012011abc" for input, and print out both "d" and "position" after the parsing. You'll see why I added that.
|
 |
 |
|
|
subject: Suggestions for date validation
|
|
|