Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse Joda time of this format

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

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.000Z back to JodaDateTime object. What should be the formatter.
I used YYYY-mm-DD HH:MM:SS and it didn't work

like image 494
Nandish A Avatar asked Jul 18 '13 13:07

Nandish A


People also ask

How do I change the date format in Joda time?

How to change the SimpleDateFormat to jodatime? String s = "2014-01-15T14:23:50.026"; DateTimeFormatter dtf = DateTimeFormat. forPattern("yyyy-MM-dd'T'HH:mm:ss. SSSS"); DateTime instant = dtf.


2 Answers

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

  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 UTC zone in formatter:

  String startDate = "2013-07-12T18:31:01.000Z";
  DateTime dt = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC).parseDateTime(startDate);
like image 76
Ilya Avatar answered Oct 23 '22 00:10

Ilya


You should append X to your pattern. The SimpleDateFormat API contains a full list of fields you can use in a DateFormat, including X for "ISO 8601 time zone".

An ISO 8601 time zone is specified as

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
like image 3
mthmulders Avatar answered Oct 23 '22 01:10

mthmulders