File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Suggestions for date validation

 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suggestions???
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that PatienceIsAVirtue!

I would use SimpleDateFormat.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure it is, with a bit of tweaking:
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope it still prints the wrong values:
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was a mistake from my side. It worked perfectly fine. Thanks!
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:It was a mistake from my side. It worked perfectly fine. Thanks!

You're welcome
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parse to Date, then convert to Calendar, then retrieve the information you need:
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestions. Would try that you!
 
Gaurav Kr. Arora
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using "01012011abc" for input, and print out both "d" and "position" after the parsing. You'll see why I added that.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic