Java JodaTime 字符串 yyyy-mm-ddThh:mmss.Z 到 DateTime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21839143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:00:31  来源:igfitidea点击:

JodaTime String yyyy-mm-ddThh:mmss.Z to DateTime

javadatetimejodatime

提问by sn0ep

hi i am using Joda time to convert my string dates to DateTime objects.

嗨,我正在使用 Joda 时间将我的字符串日期转换为 DateTime 对象。

I currently have the following string:

我目前有以下字符串:

2014-02-16T00:17:20.000Z

how do i convert this to a DateTime object?

我如何将其转换为 DateTime 对象?

I have tried:

我试过了:

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZZZ");
        DateTime dt = formatter.parseDateTime("2014-02-16T00:17:20.000Z");

But i am getting the following error:

但我收到以下错误:

java.lang.IllegalArgumentException: Invalid format: "2014-02-16T00:17:20.000Z" is malformed at ".000Z"

Any help is greatly appreciated

任何帮助是极大的赞赏

采纳答案by Bresiu

For future visitors, simpler solution:

对于未来的访问者,更简单的解决方案:

String date = "2014-02-16T00:17:20.000Z";
DateTime dateTime = new DateTime(date);

回答by JB Nizet

This format happens to be the ISO date time format, that DateTime uses by default. You just need

这种格式恰好是 DateTime 默认使用的 ISO 日期时间格式。您只需要

DateTime d = DateTime.parse(s);

or

或者

DateTime d = DateTime.parse(s, ISODateTimeFormat.dateTimeParser());

回答by Shashi Pandey

Might be issue is you guys using Z(zone) in caps

可能是你们在大写字母中使用 Z(zone) 的问题

i have tested below code works well

我已经测试过下面的代码运行良好

SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz", Locale.ENGLISH);  
Date date =formatter.parse("2016-09-06T08:35:02.530GMT"); 
DateTime d = new DateTime(date.getTime());