Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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());

like image 845
Huy Tower Avatar asked Jun 04 '14 15:06

Huy Tower


2 Answers

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

String formattedDate = dateFormat.format(someDate.getTime());
like image 56
wvdz Avatar answered Nov 16 '22 00:11

wvdz


As one of the answers here: Using GregorianCalendar with SimpleDateFormat says "A SimpleDateFormat, as its name indicates, formats Dates."

So, try this:

String formattedDate = dateFormat.format(someDate.getDate());
like image 28
Zoran Avatar answered Nov 16 '22 02:11

Zoran