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
Admin User 10161Admin User 10161 

Convert a String to Datetime

Hi there,
When I try to convert String to Datetime in Apex via the following code:
Integer randomNumber = Integer.valueOf((Math.random() * 29))+1;
Datetime dt = Datetime.parse('6/'+randomNumber+'/2020');
I have this error message: System.TypeException: Invalid date/time: 6/20/2020
Do you know how to fix that?
Best regards,
David Zhu 🔥David Zhu 🔥
Datetime parse method requires format with hour and miniute.
Datetime dt = Datetime.parse('6/'+randomNumber+'/2020, 00:00 AM');

or you can use date parse method
Date dt = Date.parse('6/'+randomNumber+'/2020');