-1

I have string in this format "2015-11-18T00:00:00+0000". I thought it's a ISO8601 format and tried to parse it to a Joda DateTime instance, but it told me it's malformed:

String toParse = "2015-11-18T00:00:00+0000";
DateTime date = ISODateTimeFormat.dateTime().parseDateTime(toParse);

And I got this error:

java.lang.IllegalArgumentException: Invalid format: "2015-11-18T00:00:00+0000" is malformed at "+0000"

How can I convert the above string to a DateTime?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user468587
  • 4,799
  • 24
  • 67
  • 124
  • 6
    Possible duplicate of [How parse 2013-03-13T20:59:31+0000 date string to Date](http://stackoverflow.com/questions/15433377/how-parse-2013-03-13t2059310000-date-string-to-date) – Malkus Jan 30 '16 at 01:48
  • 1
    @Malkus Not a duplicate, at least not related to the link you have given. It is a Joda-specific problem, see my answer. – Meno Hochschild Jan 30 '16 at 15:49
  • @Malkus by no way this is a duplicate, it's about different framework – Boris Treukhov May 25 '17 at 11:55

1 Answers1

3

The method ISODateTimeFormat.dateTime() requires a millisecond part which is missing in your input. Solution: Use the method dateTimeNoMillis().

String input = "2015-11-18T00:00:00+0000";
DateTime dt = ISODateTimeFormat.dateTimeNoMillis().parseDateTime(input);
System.out.println(dt); // 2015-11-18T01:00:00.000+01:00 (using offset of default timezone)

If you want to preserve the offset (+0000) contained in your input then you will also need to call withOffsetParsed() on your formatter.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126