[SalesForce] System.TypeException: Invalid date/time: 2019-01-03T00:26:01.711-02:00

Im trying to format this String into DateTime.

String

2019-01-03T00:26:01.711-02:00

Code

 String data = '2019-01-03T00:26:01.711-02:00';
 Datetime dt = DateTime.parse(data);

Exception

System.TypeException: Invalid date/time: 2019-01-03T00:26:01.711-02:00

Best Answer

To parse DateTimes in this ISO format, you need to use JSON deserialization.

String data = '2019-01-03T00:26:01.711-02:00';
Datetime dt = (DateTime)JSON.deserialize('"' + data + '"', DateTime.class);

The JSON parser understands ISO format, while DateTime.parse() uses the running user's locale settings.

Related Topic