JodaTime String yyyy-mm-ddThh:mmss.Z to DateTime

14,623

Solution 1

For future visitors, simpler solution:

String date = "2014-02-16T00:17:20.000Z";
DateTime dateTime = new DateTime(date);

Solution 2

This format happens to be the ISO date time format, that DateTime uses by default. You just need

DateTime d = DateTime.parse(s);

or

DateTime d = DateTime.parse(s, ISODateTimeFormat.dateTimeParser());

Solution 3

Might be issue is you guys using Z(zone) in caps

i have tested below code works well

SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz", Locale.ENGLISH);  
Date date =formatter.parse("2016-09-06T08:35:02.530GMT"); 
DateTime d = new DateTime(date.getTime());
Share:
14,623
sn0ep
Author by

sn0ep

Updated on July 27, 2022

Comments

  • sn0ep
    sn0ep almost 2 years

    hi i am using Joda time to convert my string dates to DateTime objects.

    I currently have the following string:

    2014-02-16T00:17:20.000Z
    

    how do i convert this to a DateTime object?

    I have tried:

    DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZZZ");
            DateTime dt = formatter.parseDateTime("2014-02-16T00:17:20.000Z");
    

    But i am getting the following error:

    java.lang.IllegalArgumentException: Invalid format: "2014-02-16T00:17:20.000Z" is malformed at ".000Z"
    

    Any help is greatly appreciated

  • Aubergine
    Aubergine over 9 years
    I don't see how is this helpful. Needed a pattern and the one provided in comments does not work.
  • Dez Udezue
    Dez Udezue over 7 years
    how is this helpful? The question is asking about DateTimeFormat.
  • Bresiu
    Bresiu over 7 years
    @DezUdezue The question is asking how to convert string in a specific format to DateTime object.