如何将日期转换为或(Mon Jan 12 00:00:00 IST 2015)
格式?MM.dd.yyyy
dd.MM.yyyy
我尝试使用以下方法
String dateStr = "Mon Jan 12 00:00:00 IST 2015";
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
System.out.println(formatter.format(dateStr));
Run Code Online (Sandbox Code Playgroud)
但得到了
Exception in thread "main" java.lang.IllegalArgumentException:
Cannot format given Object as a Date
Run Code Online (Sandbox Code Playgroud)
您必须将字符串解析为日期,然后格式化该日期
String dateStr = "Mon Jan 12 00:00:00 IST 2015";
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
DateFormat formatter1 = new SimpleDateFormat("dd.MM.yyyy");
System.out.println(formatter1.format(formatter.parse(dateStr)));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
50785 次 |
最近记录: |