Using GregorianCalendar with SimpleDateFormat
SimpleDateFormat.format()
method takes a Date
as a parameter. You can get a Date
from a Calendar
by calling its getTime()
method:
public static String format(GregorianCalendar calendar) {
SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");
fmt.setCalendar(calendar);
String dateFormatted = fmt.format(calendar.getTime());
return dateFormatted;
}
Also note that the months start at 0, so you probably meant:
int month = Integer.parseInt(splitDate[1]) - 1;
Why such complications?
public static GregorianCalendar convertFromDMY(String dd_mm_yy) throws ParseException
{
SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");
Date date = fmt.parse(dd_mm_yy);
GregorianCalendar cal = GregorianCalendar.getInstance();
cal.setTime(date);
return cal;
}