在java中格式化日期 [英] Formatting date in java

查看:137
本文介绍了在java中格式化日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试格式化日期字符串ex。 2014-11-24T18:30:00.000Z 2014-11-24 使用以下代码:

I am trying to format date string ex. 2014-11-24T18:30:00.000Z to 2014-11-24 using this code below:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
dateFormat.format(reqJsonObj.getString(FROM_DATE));

但它引发异常

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


推荐答案

使用 dateFormat.parse()而不是 dateFormat.format(),因为您想要将 String 解析为 Date 对象。然后,当您拥有 Date 对象时,请将其格式化为 String 并使用所需格式。 @Jens已经为您提供了完整的代码,因此无需在此处再次复制。

Use dateFormat.parse() instead of dateFormat.format(), since you want to parse your String into Date object. Then, when you have the Date object, format it to String with wanted format. @Jens already gave you the full code, so no need to copy it again here.

这篇关于在java中格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆