Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is date valid

 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need to check if date is valid
This is the format of the date: ####-##-## ##:##:## ,for example2008-12-23 09:29:13
I am a bit confused with all the date objects in java
Any suggestion regarding how should I check this format ?

Thanks

 
Sunil Kumar
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use simple date format like this

 
Mustafa Musaji
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if I completely understand how you want to check the date, but you can use SimpleDateFormat to create the format of the date and then parse a date object using the formatter created. Check out the API for SimpleDateFormat or google "date format using Java". You'll find some good examples.

EDIT: Exactly how Sunil has said above! lol, beat me to it!
 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Mustafa Musaji
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may also want to check out this Thread in the intermediate forum

http://www.coderanch.com/t/425542/Java-General-intermediate/Date-validation
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
I've a dummy doubt.
Please don't mind.
In the below code,


datestr is the original date value & testDate(in the if condition after it is executed) is the formatted date.
There is likely both may not be equal .
in that case if block becomes true right & returns false.
So the original date & validated date won't be equal right!
 
Sunil Kumar
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ramya narayanan wrote:There is likely both may not be equal .

Not sure if this case exists. Did you find one?
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No sunil, I want to know what the below code does.

Regards
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunil Kumar wrote:Use simple date format like this


There are two improvements that can be made:
- turn off leniency
- check if the parsing did not ignore the trailing part of the string (ok, you're actually doing that at the end, but it can be a bit more efficient ;))


If the string could not be parsed, then pos.getIndex() will still return 0. If it could be parsed but there is trailing garbage (e.g. "2009-01-13 09:34:00 Hello world!") then pos.getIndex() will be smaller than dateStr.length().

As for no exception, that's because DateFormat.parse(String) uses the same technique:

And if you can avoid an exception, you should always do so.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ramya narayanan wrote:No sunil, I want to know what the below code does.

Regards

Parsing a date can remove any trailing garbage, as in the example I described above. This statement checks if the formatted date is equal to the source string. Taking my example, the formatted date would be "2009-01-13 09:34:00" which is not equal to the source.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob Prime for once again patiently explaining the best practices !
My Best Regards(to you)!
 
Sunil Kumar
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic