JodaTime how to parse ISO 8601 time string into DateTime?

i have the string with time in the following format:

2016-01-07T08:00:00+00:00

When i trying to parse string using following method.

public static DateTime getDateTimeObject(String dateTime) {
        //DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(PATTERN);
        //DateTime dateTimeObj = dateTimeFormatter.parseDateTime(dateTime);
        Logger.d(dateTime);
        DateTime dateTimeObj = null;
        try {
            dateTimeObj = ISODateTimeFormat.dateTime().parseDateTime(dateTime);
            return dateTimeObj;
        } catch (Exception e) {
            Logger.e(e.getMessage());
        }
        return dateTimeObj;
    }

I always get following exception.

Invalid format: "2016-01-07T08:00:00+00:00" is malformed at "+00:00"

How can i parse string in ISO format to get valid DateTime object?

Jon Skeet
people
quotationmark

Your value doesn't have a milliseconds component, so you want ISODateTimeFormat.dateTimeNoMillis():

Returns a formatter that combines a full date and time without millis, separated by a 'T' (yyyy-MM-dd'T'HH:mm:ssZZ).

The dateTime() method returns a formatter with a format of yyyy-MM-dd'T'HH:mm:ss.SSSZZ which your string doesn't comply with.

people

See more on this question at Stackoverflow