Question

I'm having an IllegalArgumentException when I validate a date on a DatePickerDialog. Here is the stacktrace:

10-27 19:50:34.700: E/AndroidRuntime(1188): FATAL EXCEPTION: main
10-27 19:50:34.700: E/AndroidRuntime(1188): java.lang.IllegalArgumentException
10-27 19:50:34.700: E/AndroidRuntime(1188):     at java.text.DateFormat.format(DateFormat.java:365)
10-27 19:50:34.700: E/AndroidRuntime(1188):     at java.text.Format.format(Format.java:93)
10-27 19:50:34.700: E/AndroidRuntime(1188):     at com.boka.lesbonscomptes.ActivityMain$1.onDateSet(ActivityMain.java:43)
10-27 19:50:34.700: E/AndroidRuntime(1188):     at android.app.DatePickerDialog.onClick(DatePickerDialog.java:111)
10-27 19:50:34.700: E/AndroidRuntime(1188):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
10-27 19:50:34.700: E/AndroidRuntime(1188):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-27 19:50:34.700: E/AndroidRuntime(1188):     at android.os.Looper.loop(Looper.java:137)
10-27 19:50:34.700: E/AndroidRuntime(1188):     at android.app.ActivityThread.main(ActivityThread.java:4424)
10-27 19:50:34.700: E/AndroidRuntime(1188):     at java.lang.reflect.Method.invokeNative(Native Method)
10-27 19:50:34.700: E/AndroidRuntime(1188):     at java.lang.reflect.Method.invoke(Method.java:511)
10-27 19:50:34.700: E/AndroidRuntime(1188):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-27 19:50:34.700: E/AndroidRuntime(1188):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-27 19:50:34.700: E/AndroidRuntime(1188):     at dalvik.system.NativeStart.main(Native Method)

and my code

    public class ActivityMain extends Activity {

    //Attributs UI
    private LinearLayout layoutNouvelleDepense = null;
    private Button bDate = null;

    private GregorianCalendar gcDate = null;

    //util
    private SimpleDateFormat sdfAjd = null;
    static final int DATE_DIALOG_ID = 1;


    /*LISTENER*/

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
        {
            Toast.makeText(getBaseContext(), "ok",Toast.LENGTH_SHORT).show();
            //La date est calée sur le choix de l'utilisateur
            gcDate.set(year, monthOfYear, dayOfMonth);
            //Le bouton est mis à jour avec la date choisie
            bDate.setText(sdfAjd.format(gcDate));
        }
    };  

    private OnClickListener oclBoutonDate = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);     
        }
    };      


    /*METHODES*/
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        layoutNouvelleDepense = (LinearLayout) LinearLayout.inflate(this,R.layout.nouvelle_depense, null);
        bDate = (Button) layoutNouvelleDepense.findViewById(R.id.bDate);

        bDate.setOnClickListener(oclBoutonDate);

        gcDate = new GregorianCalendar();
        sdfAjd = new SimpleDateFormat("EEEE dd/MM/yyyy", Locale.getDefault());

        bDate.setText(sdfAjd.format(gcDate.getTime()));

        setContentView(layoutNouvelleDepense);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @SuppressWarnings("static-access")
    @Override    
    protected Dialog onCreateDialog(int id) 
    {
        System.out.println("DATE = " + gcDate.get(GregorianCalendar.YEAR) + "/" + gcDate.get(GregorianCalendar.MONTH) + "/" +  gcDate.get(GregorianCalendar.DAY_OF_MONTH));
         return new DatePickerDialog(this, mDateSetListener, gcDate.get(GregorianCalendar.YEAR), gcDate.get(GregorianCalendar.MONTH), gcDate.get(GregorianCalendar.DAY_OF_MONTH));
    }
}

I have no issues in creating the dialog, and the date is correctly initialized when the dialog is displayed.

No matter the date chosen, the IllegalArgumentException is raised.

I've been looking for an answer since quite a while, but all the similar posts are about excepetion raised at the creation of the dialog, not the validation of the date.

Can you please help me find out what's going on ?

Thank you

Was it helpful?

Solution

In your OnDateSetListener add getTime() to your format command:

bDate.setText(sdfAjd.format(gcDate.getTime()));

The difference is that GregorianCalendar#getTime() returns a Date object and calls SimpleDateFormat#format(Date) which, as you know, works fine in your onCreate() method.

But format(gcDate) by itself calls the generic SimpleDateFormat#format(Object) method which throws the exception because this format() does not understand the Date object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top