簡體   English   中英

使用JODA將RFC 3339從string解析為java.util.Date

[英]Parse RFC 3339 from string to java.util.Date using JODA

假設我有一個為RFC 3339格式化的字符串日期,例如由以下代碼生成的“2013-07-04T23:37:46.782Z”:

// This is our date/time
Date nowDate = new Date();
// Apply RFC3339 format using JODA-TIME
DateTime dateTime = new DateTime(nowDate.getTime(), DateTimeZone.UTC);
DateTimeFormatter dateFormatter = ISODateTimeFormat.dateTime();
String dateString = dateFormatter.print(dateTime);
System.out.println("Server side date (RFC 3339): " + dateString );
// Server side date (RFC 3339): 2013-07-04T23:37:46.782Z

現在我想使用JODA-TIME從我的字符串“2013-07-04T23:37:46.782Z”創建一個java.util.Date。 我如何實現這一目標?

問題的實際答案(Yori:您使用ISODateTimeFormat是對的,但您的代碼/接受的答案會進行格式化,而不是解析):

public static java.util.Date Rfc3339ToDateThroughJoda(String dateString) {
    DateTimeFormatter dateFormatter = ISODateTimeFormat.dateTime();   
    DateTime dateTime = dateFormatter.parseDateTime(dateString);    
    return dateTime.toDate();
}

好吧,我找到了解決方案。 它在我的鼻子底下。

// Apply RFC3339 format using JODA-TIME
DateTime dateTime = new DateTime("2013-07-04T23:37:46.782Z", DateTimeZone.UTC);
DateTimeFormatter dateFormatter = ISODateTimeFormat.dateTime();

希望它可以幫助別人。

暫無
暫無

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

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