java.lang.IllegalArgumentException: Bad class: class java.util.GregorianCalendar

Huy Tower picture Huy Tower · Jun 4, 2014 · Viewed 20.2k times · Source

I received this exception while using GregorianCalendar

java.lang.IllegalArgumentException: Bad class: class java.util.GregorianCalendar

Who know how to fix,

Please help me.

p/s : I used the following code :

Calendar someDate = GregorianCalendar.getInstance();
        someDate.add(Calendar.DAY_OF_YEAR, -7);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = dateFormat.format(someDate);

UPDATED I should be use this line to achieve the date time :

String formattedDate = dateFormat.format(someDate.getTime());

Answer

wvdz picture wvdz · Jun 4, 2014

A Calendar can't be directly formatted, you need to get the Date from the Calendar, like this:

String formattedDate = dateFormat.format(someDate.getTime());