java.lang.IllegalArgumentException: Bad class: class java.util.GregorianCalendar
20,496
Solution 1
A Calendar can't be directly formatted, you need to get the Date from the Calendar, like this:
String formattedDate = dateFormat.format(someDate.getTime());
Solution 2
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());
Related videos on Youtube
![Huy Tower](https://i.stack.imgur.com/pqR4l.jpg?s=256&g=1)
Author by
Huy Tower
Huy Tower --- Flutter Developer | Senior Android Developer --- Skype: huytower Location : Ho Chi Minh, Viet Nam
Updated on July 09, 2022Comments
-
Huy Tower almost 2 years
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());
-
wvdz about 10 yearsIf using eclipse: remove your imports, then do ctrl+Shift+O
-
Huy Tower about 10 yearshuh? don't understand your case clearly. But after do thing as u said. still can not fix issu
-
Pratik Butani almost 9 yearsSame What i want to ask :P
-
Huy Tower almost 8 yearsYeah, many people need it :p
-
-
Huy Tower almost 10 years
someDate.getTime()
will be returnDate
, notsomeDate.getDate()
-
xwild over 8 yearsActually it's not so good to call a method like this, signature:
public final Date getTime()
(I expect get date method which will return time :) -
wvdz over 8 years@xwild What do you mean?
-
xwild over 8 yearsI mean calendar methods naming are not so good, getTime should return Time (i.e. 12:00), getDate shoud return Date (i.e. 2016-01-01), but now in java
getTime()
returns aDate
instance. -
Peter Chaula over 7 years
Calendar
is not a subclass ofDate
. Why does this compile on Android? -
wvdz over 7 years@peter:
Calendar.getTime()
retrieves theCalendar
as aDate
. -
Peter Chaula over 7 years@wvdz I mean
SimpleDateFormat::format(Calendar)
. It compiled on my Android app but then it threw a a runtime exception -
wvdz over 7 years@peter.
SimpleDateFormat
extendsFormat
which hasformat(Object)
.