• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Barry Brashear
Ranch Hand
Posts: 303
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can you get a Date object from a string in the format "dd/mm/yyyy"?

Thanks.
 
Kip Dekan
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Bob Reardon
Ranch Hand
Posts: 161
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic