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());
Author by
sn0ep
Updated on July 27, 2022Comments
-
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 over 9 yearsI don't see how is this helpful. Needed a pattern and the one provided in comments does not work.
-
Dez Udezue over 7 yearshow is this helpful? The question is asking about DateTimeFormat.
-
Bresiu over 7 years@DezUdezue The question is asking how to convert string in a specific format to DateTime object.