aspose file tools*
The moose likes Java in General and the fly likes Suggestions for date validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Suggestions for date validation" Watch "Suggestions for date validation" New topic
Author

Suggestions for date validation

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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: 19720
    
  20

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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: 19720
    
  20

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: 9427
    
    2

Nope it still prints the wrong values:
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

It was a mistake from my side. It worked perfectly fine. Thanks!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

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: 19720
    
  20

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: 9427
    
    2

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: 19720
    
  20

Parse to Date, then convert to Calendar, then retrieve the information you need:
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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: 19720
    
  20

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: 9427
    
    2

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: 19720
    
  20

Try using "01012011abc" for input, and print out both "d" and "position" after the parsing. You'll see why I added that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Suggestions for date validation