简体   繁体   English

SimpleDateFormat引发异常

[英]SimpleDateFormat throws exception

I want to show a date string in this format: Jun 27, 2012 . 我想以这种格式显示日期字符串: Jun 27, 2012 I did this: 我这样做:

SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyy");
String date = sdf.format(Calendar.getInstance());

But it gives me this: 但这给了我这个:

java.lang.IllegalArgumentException: Cannot format given Object as a Date

What am I doing wrong? 我究竟做错了什么?

sdf.format()

采用Date而不是Calendar对象。

You can not use Calendar.getInstance() , since the param for same is date and not calender instance. 您不能使用Calendar.getInstance() ,因为相同的参数是日期,而不是日历实例。 Just change the param to getTime() 只需将参数更改为getTime()

SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
String date = sdf.format(Calendar.getInstance().getTime());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM