I'm using the following code to convert a date format from a DatePicker
:
public void onDateSet(DatePicker view, int year, int month, int day) {
System.out.println("year=" + year + "day=" + day + "month="
+ month);
String myFormat = "dd-M-yyyy";
String dateStr = day + "-" + month + "-" + year;
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.ENGLISH);
SimpleDateFormat originalFormat = new SimpleDateFormat(myFormat, Locale.ENGLISH);
SimpleDateFormat targetFormat = new SimpleDateFormat("dd,MMMM yyyy");
Date date = originalFormat.parse(dateStr);
String formattedDate = targetFormat.format(date);
System.out.println(formattedDate);
}
Unfortunately it's throwing an error:
Error:(115, 45) error: unreported exception ParseException; must be caught or declared to be thrown
I tried adding a throws
to it:
public void onDateSet(DatePicker view, int year, int month, int day) throws ParseException {
System.out.println("year=" + year + "day=" + day + "month="
+ month);
String myFormat = "dd-M-yyyy";
String dateStr = day + "-" + month + "-" + year;
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.ENGLISH);
SimpleDateFormat originalFormat = new SimpleDateFormat(myFormat, Locale.ENGLISH);
SimpleDateFormat targetFormat = new SimpleDateFormat("dd,MMMM yyyy");
String effDate = targetFormat.format(originalFormat.parse(dateStr));
System.out.println(effDate);
}
New error is shown as
Error:(106, 21) error: onDateSet(DatePicker,int,int,int) in DatePickerFragment cannot implement onDateSet(DatePicker,int,int,int) in OnDateSetListener, overridden method does not throw ParseException