java 如何解析这种格式的Joda时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17725168/
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-11-01 14:47:30  来源:igfitidea点击:

How to parse Joda time of this format

javaHymansonjodatime

提问by Nandish A

I converted DateString of format YYYY-mm-DD HH:MM:SSin my JSON and persisted to POJO using the code

YYYY-mm-DD HH:MM:SS在我的 JSON 中转换了 DateString 格式并使用代码持久化到 POJO

DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
this.startDate = format.parseDateTime(startDate);

When I convert the POJO back to JSON, the date is written like 2013-07-12T18:31:01.000Z.
How do we parse the time string 2013-07-12T18:31:01.000Zback to JodaDateTime object. What should be the formatter.
I used YYYY-mm-DD HH:MM:SSand it didn't work

当我将 POJO 转换回 JSON 时,日期写成2013-07-12T18:31:01.000Z.
我们如何将时间字符串解析2013-07-12T18:31:01.000Z回 JodaDateTime 对象。格式化程序应该是什么。
我用过YYYY-mm-DD HH:MM:SS,没用

回答by Ilya

2013-07-12T18:31:01.000Zit is standart ISO date time format.
You can use standart Joda date time formatter ISODateTimeFormat::dateTime()
Example:

2013-07-12T18:31:01.000Z它是标准的 ISO 日期时间格式。
您可以使用标准 Joda 日期时间格式化程序ISODateTimeFormat::dateTime()
示例:

  String startDate = "2013-07-12T18:31:01.000Z";
  DateTime dt = ISODateTimeFormat.dateTime().parseDateTime(startDate);  

in this case date will be converted to date in your time zone.
If you want ignore your time zone use UTCzone in formatter:

在这种情况下,日期将转换为您所在时区的日期。
如果您想忽略您的时UTC区,请在格式化程序中使用zone:

  String startDate = "2013-07-12T18:31:01.000Z";
  DateTime dt = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC).parseDateTime(startDate);

回答by mthmulders

You should append Xto your pattern. The SimpleDateFormatAPIcontains a full list of fields you can use in a DateFormat, including Xfor "ISO 8601 time zone".

您应该附加X到您的模式。该SimpleDateFormatAPI包含字段,你可以在使用的完整列表DateFormat,其中包括X了“ISO 8601时区”。

An ISO 8601 time zone is specified as

ISO 8601 时区指定为

The number of pattern letters designates the format for both formatting and parsing as follows:

 ISO8601TimeZone:
         OneLetterISO8601TimeZone
         TwoLetterISO8601TimeZone
         ThreeLetterISO8601TimeZone
 OneLetterISO8601TimeZone:
         Sign TwoDigitHours
         Z
 TwoLetterISO8601TimeZone:
         Sign TwoDigitHours Minutes
         Z
 ThreeLetterISO8601TimeZone:
         Sign TwoDigitHours : Minutes
         Z

模式字母的数量指定用于格式化和解析的格式如下:

 ISO8601TimeZone:
         OneLetterISO8601TimeZone
         TwoLetterISO8601TimeZone
         ThreeLetterISO8601TimeZone
 OneLetterISO8601TimeZone:
         Sign TwoDigitHours
         Z
 TwoLetterISO8601TimeZone:
         Sign TwoDigitHours Minutes
         Z
 ThreeLetterISO8601TimeZone:
         Sign TwoDigitHours : Minutes
         Z

回答by user1708042

The API doc is not entirely clear: one should read

API 文档并不完全清楚:应该阅读

OneLetterISO8601TimeZone means 'X' and yields Sign TwoDigitHours or Z,

OneLetterISO8601TimeZone 表示“X”并产生符号 TwoDigitHours 或 Z,

hence something like -05

因此像 -05

TwoLetterISO8601TimeZone means 'XX' and yields Sign TwoDigitHours Minutes or Z,

TwoLetterISO8601TimeZone 表示“XX”并产生符号 TwoDigitHours Minutes 或 Z,

hence something like -0500

因此类似于 -0500

and ThreeLetterISO8601TimeZone means 'XXX' and yields Sign TwoDigitHours : Minutes

和 ThreeLetterISO8601TimeZone 表示 'XXX' 并产生 Sign TwoDigitHours : Minutes

hence something like -05:00

因此类似于 -05:00