Let's suppose that I have a date as string formatted for RFC 3339 such as "2013-07-04T23:37:46.782Z" generated by the code below:
// 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 a java.util.Date from my string "2013-07-04T23:37:46.782Z" using JODA-TIME. How do I achieve that?
Parsing String to Date in Java using Joda TimeCreate a date pattern using forPattern() method of DateTimeFormat class. forPattern() method returns an object of DateTimeFormatter which does actual parsing. Use parseMillis() method of DateTimeFormatter class to convert String to long millisecond.
Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters. Mid level - pattern-based, like SimpleDateFormat. Low level - builder.
ISO 8601, RFC 2822 and RFC 3339 are a standards for date and time representation covering the formatting of date and time (with or without possible fractional seconds) and timezone information.
Conclusion. Class java. util. Date stores a date-time value as milliseconds since the epoch.
Actual answer to question (Yori: you're right in using 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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With