• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to check the format of 2 dates

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have get the locale of the user like i have get its date format :

yyyy/mm/dd

now the user will enter the date in any format , now i have to check weather the date entered by him is in proper format or not. Is there any methord in java to check the format of the user and date entered by him.

Thanks
Regards
Gaurav
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd set up a DateFormat and try to parse() the string entered by the user. It will throw an exception for invalid data. If you were running a batch of these things, say importing millions of rows into a database, the overhead of try-throw-catch might be a factor, but I'd have to prove it's a performance problem before I tried to replace it with regular expressions and my own checking.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

now the user will enter the date in any format

Any format? Any format at all? No, there's no method for guessing what date the user might have meant by a particular string. There are far too many possibilities and far too many ambiguities. You should specify to the user what format to use.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using the DateFormat.parse to validate input, don�t forget to set the lenitent property to true in the DateFormat instance after creation, this is because DateFormat is somewhat flexible and it will try to adapt to uncertain input to get a good date even if it is wrong. The lenitent property assures it will be more strict in parsing.
 
reply
    Bookmark Topic Watch Topic
  • New Topic