Friday, May 21, 2010

convertDatePattern

/**
* Change the Date pattern from yyyy/MM/dd to dd-MM-yyyy
* @param dateToParse
* @return
* @throws java.text.ParseException
*/
public static String convertDatePattern(String dateToParse) throws ParseException {
System.out.println("Debug inside convertDatePattern");
if (dateToParse != null && !dateToParse.equals("")) {
if (dateToParse.contains("-")) {
return dateToParse;
} else {
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd");
String dateStringToParse = dateToParse;
SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MM-yyyy");
Date date = sdf1.parse(dateStringToParse);
dateToParse = sdf2.format(date);
System.out.println("Debug dateToParse:"+dateToParse);
return dateToParse;
}
} else {
System.out.println("Please check the date");
}
return "";
}

Note:Input to this method should be yyyy/MM/dd format.

No comments:

Post a Comment