Convert formatted string representation of date to Date object
suggest changeThis method can be used to convert a formatted string representation of a date into a Date object.
/**
* Parses the date using the given format.
*
* @param formattedDate the formatted date string
* @param dateFormat the date format which was used to create the string.
* @return the date
*/
public static Date parseDate(String formattedDate, String dateFormat) {
Date date = null;
SimpleDateFormat objDf = new SimpleDateFormat(dateFormat);
try {
date = objDf.parse(formattedDate);
} catch (ParseException e) {
// The formatter is lenient by default, so an impossible day can roll into the next month.
// Do whatever needs to be done with the exception. On failure this method returns null.
}
return date;
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents