• 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

Getting a Date from an string "mm/dd/yyyy'

 
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can you get a Date object from a string in the format "dd/mm/yyyy"?

Thanks.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would try that code seqenze below.

Perhaps you've got to change the dateformat "dd.MM.yyyy" -> "dd/MM/yyyy", but it should work.

code:
private java.util.Date string2date(java.lang.String fieldName, java.lang.String dateString) throws java.text.ParseException
{
private java.text.DateFormat DEFAULT_DATE_FORMAT = new java.text.SimpleDateFormat("dd.MM.yyyy");
if (!(dateString == null || dateString.trim().length() == 0))
{
return DEFAULT_DATE_FORMAT.parse(dateString.trim());
}
return null;
}
[ October 19, 2004: Message edited by: Kip Dekan ]
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One other thing. Be sure to set lenient to false:

DEFAULT_DATE_FORMAT.setLenient(false);

This will ensure you have a correct and valid date. By setting Lenient to false the parser will check that you have entered the correct values for the day and month field. ie the month field is between 1 and 12 and the day field is a valid value for each month. So if a user tries to enter 31/02/2004, the parser will reject it as an invalid date.
 
reply
    Bookmark Topic Watch Topic
  • New Topic