8

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

sn0ep
  • 3,843
  • 8
  • 39
  • 63

3 Answers3

23

For future visitors, simpler solution:

String date = "2014-02-16T00:17:20.000Z";
DateTime dateTime = new DateTime(date);
Bresiu
  • 2,123
  • 2
  • 19
  • 31
17

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());
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

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());
Kerem Baydoğan
  • 10,475
  • 1
  • 43
  • 50