File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Getting a Date from an string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting a Date from an string "mm/dd/yyyy Watch "Getting a Date from an string "mm/dd/yyyy New topic

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

Barry Brashear
Ranch Hand

Joined: Jun 05, 2001
Posts: 303
How can you get a Date object from a string in the format "dd/mm/yyyy"?

Kip Dekan

Joined: Apr 22, 2003
Posts: 9
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.

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

Joined: Jun 01, 2000
Posts: 161
One other thing. Be sure to set lenient to 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:
subject: Getting a Date from an string "mm/dd/yyyy'
It's not a secret anymore!