DateFormat conversion problem in java?

孤人 提交于 2019-11-29 08:19:59

The problem is in this part:

new Date("2010-03-24T17:28:50.000Z")

Apparently it doesn't accept dates/times in that format.

You shouldn't be using that constructor anyway - create an appropriate formatter to parse that particular format, and then parse it with that.

Alternatively, use Joda Time to start with, and avoid using DateFormat completely. I don't know if you can use Joda Time from Android, mind you... and it's fairly large.

EDIT: To spell it out explicitly:

String inputText = "2010-03-24T17:28:50.000Z";
// "Z" appears not to be supported for some reason.
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
DateFormat outputFormat = new SimpleDateFormat("EEE. MMM. d. yyyy");
Date parsed = inputFormat.parse(inputText);
String outputText = outputFormat.format(parsed);

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