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

南笙酒味 提交于 2019-12-18 14:09:14

问题


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


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



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


来源:https://stackoverflow.com/questions/24041513/java-lang-illegalargumentexception-bad-class-class-java-util-gregoriancalendar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!