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,759
  • 8
  • 36
  • 60

3 Answers3

23

For future visitors, simpler solution:

String date = "2014-02-16T00:17:20.000Z";
DateTime dateTime = new DateTime(date);
Bresiu
  • 1,945
  • 2
  • 18
  • 30
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
  • 657,433
  • 87
  • 1,179
  • 1,226
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,172
  • 1
  • 40
  • 49