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

Hi, I am using Joda time to convert string dates to DateTime objects.

I currently have the following line:

2014-02-16T00:17:20.000Z

How to convert this to a DateTime object?

I tried:

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

But I get the following error:

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

Any help is appreciated

+4
source share
3 answers

For future visitors a simpler solution:

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

This format is the ISO date date format, which uses DateTime by default. You just need

DateTime d = DateTime.parse(s);

or

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

, Z ()

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());
+1

Source: https://habr.com/ru/post/1527380/


All Articles