簡體   English   中英

如何解析這種格式的Joda時間

[英]How to parse Joda time of this format

我在我的JSON中轉換了格式YYYY-mm-DD HH:MM:SS DateString,並使用代碼保存到POJO

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

當我將POJO轉換回JSON時,日期寫成2013-07-12T18:31:01.000Z
我們如何將時間字符串2013-07-12T18:31:01.000Z回JodaDateTime對象。 什么應該格式化。
我使用了YYYY-mm-DD HH:MM:SS而且沒用

2013-07-12T18:31:01.000Z它是標准的ISO日期時間格式。
您可以使用標准的Joda日期時間格式器ISODateTimeFormat :: dateTime()
例:

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

在這種情況下,日期將轉換為您所在時區的日期。
如果要忽略您的時區,請在格式化程序中使用UTC區域:

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

您應該將X附加到您的模式中。 SimpleDateFormat API包含可在DateFormat使用的完整字段列表,包括“ISO 8601時區”的X

ISO 8601時區指定為

模式字母的數量指定格式化和解析的格式,如下所示:

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

API文檔並不完全清楚:應該閱讀

OneLetterISO8601TimeZone表示'X'並產生Sign TwoDigitHours或Z,

因此像-05

TwoLetterISO8601TimeZone表示'XX'並產生Sign TwoDigitHours Minutes或Z,

因此像-0500

和ThreeLetterISO8601TimeZone的意思是'XXX'並產生Sign TwoDigitHours:Minutes

因此像-05:00

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM