RFC 3339 parsing from string to java.util.Date using JODA

Suppose I have a date as a string formatted for RFC 3339, such as "2013-07-04T23: 37: 46.782Z", generated by the following code:

// 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 

Now I want to create java.util.Date from my line "2013-07-04T23: 37: 46.782Z" using JODA-TIME. How do I achieve this?

+4
source share
2 answers

Ok, I found a solution. It was right under my nose.

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

Hope this can help someone else.

+3
source

The actual answer to the question (Yori: you are right to use ISODateTimeFormat , but your code / accepted answer does formatting, not parsing):

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

Source: https://habr.com/ru/post/1489812/


All Articles