function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Eric Blaxton 11Eric Blaxton 11 

How to deal with APEX DATE TIME type exceptions

Hello and thanks in advance,

I am trying to store a DateTime value in a DateTime variable.  I've tried the valueofGMT and got an error also. 

Returned Value from HTTP request: 
"reportDate":"2020-11-04T13:51:16.4718777-06:00"

Line causing fatal error:
Datetime reportDate= Datetime.valueOf(valueMap.get('reportDate'));

Error Code: 
10:00:25:056 FATAL_ERROR System.TypeException: Invalid date/time: 2020-11-04T13:49:57.3103458-06:00
Regards,
Eric
 
Best Answer chosen by Eric Blaxton 11
ShirishaShirisha (Salesforce Developers) 
Hi Eric,

Greetings!

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.

Reference:https://salesforce.stackexchange.com/questions/247210/system-typeexception-invalid-date-time-2019-01-03t002601-711-0200

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Eric,

Greetings!

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.

Reference:https://salesforce.stackexchange.com/questions/247210/system-typeexception-invalid-date-time-2019-01-03t002601-711-0200

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
This was selected as the best answer
Eric Blaxton 11Eric Blaxton 11
Hi Shirisha,

Thank you for your time and help.  

Here's the end result.
 
Datetime reportDate= (DateTime)JSON.deserialize('"' + valueMap.get('reportDate') + '"', DateTime.class)

Eric