-1

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

Joel G Mathew
  • 7,561
  • 15
  • 54
  • 86
  • The `parse` method throws `ParseException`, which is a checked exception - so you must put the code inside a `try/catch` block **or** add `throws ParseException` at `onDateSet` method –  Jun 30 '17 at 18:27
  • Can you throw some light on this checked exception? Do you mean that it's normal to have this exception? – Joel G Mathew Jun 30 '17 at 18:28
  • Yes, it's normal: https://docs.oracle.com/javase/tutorial/essential/exceptions/catchOrDeclare.html –  Jun 30 '17 at 18:29
  • The new error occurs because probably you're extending another class (or implementing an interface) and overriding a method, so you can't change the signature. Add a try/catch block instead –  Jun 30 '17 at 18:50
  • FYI, you are using troublesome old legacy classes now supplanted by the java.time classes. – Basil Bourque Jun 30 '17 at 20:32

1 Answers1

2

This error occurs because the parse method throws a ParseException - this exception is a checked one, and it needs to be handled properly: by putting the code in a try/catch block or declaring that the method onDateSet throws the exception (adding throws ParseException in the method declaration). In your case, as you seem to be extending a class or implementing an interface, just put the code inside a try/catch block.

Another detail is that you don't need to create 3 SimpleDateFormat instances. Just one is enough, for the output (targetFormat).

To build a date object, just use java.util.Calendar - reminding that in this API, months start at zero (yes, January is 0, so you'll probably have to subtract 1 - check if your API is getting the month correctly):

Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.DAY_OF_MONTH, day);

SimpleDateFormat targetFormat = new SimpleDateFormat("dd,MMMM yyyy", Locale.ENGLISH);
String formattedDate = targetFormat.format(cal.getTime());

New Date/time API

The old classes (Date, Calendar and SimpleDateFormat) have lots of problems and design issues, and they're being replaced by the new APIs.

If you're ok about adding a dependency to your project, in Android you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes, together with the ThreeTenABP (more on how to use it here).

All relevant classes are in the package org.threeten.bp. The code is pretty much simpler, and you don't have to worry about zero-indexed month (January is 1 in this API):

import org.threeten.bp.format.DateTimeFormatter;
import org.threeten.bp.LocalDate;

DateTimeFormatter targetFormat = DateTimeFormatter.ofPattern("dd,MMMM yyyy", Locale.ENGLISH);
String formattedDate = targetFormat.format(LocalDate.of(year, month, day));
Community
  • 1
  • 1
  • 2
    Thank you for the elaborate answer! – Joel G Mathew Jun 30 '17 at 18:56
  • 1
    The modern date and time API would in fact solve the problem even if you kept with parsing the date, since this throws unchecked exceptions on parse errors. Such need not be declared nor explicitly caught. It is of course even more elegant to avoid parsing as in the code in the answer. This too would throw an unchecked exception on invalid values. – Ole V.V. Jun 30 '17 at 20:43