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
Insane@blrInsane@blr 

Parse method in DateTime

Hi Guys,

 

I was trying to parse a specific format. its giving exception invalid Date/Time.Here the following is simple code.

Can any body help me on this issue.

 

DateTime dt = DateTime.now();

String str = dt.format('yyyy/MM/dd hh:mm:ss ');

dateTime mydate = dateTime.parse(str);

Error :

 

DEBUG LOG

20.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO

Execute Anonymous: DateTime dt = DateTime.now();

Execute Anonymous: String str = dt.format('yyyy/MM/dd hh:mm:ss ');

Execute Anonymous: dateTime mydate = dateTime.parse(str);

19:52:19.039 (39170000)|EXECUTION_STARTED

19:52:19.039 (39217000)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex

19:52:19.040 (40663000)|EXCEPTION_THROWN|[3]|System.TypeException: Invalid date/time: 2011/08/29 07:52:19

19:52:19.040 (40947000)|FATAL_ERROR|System.TypeException: Invalid date/time: 2011/08/29 07:52:19

 

aayushaayush

The following code works for me:

 



DateTime dt = DateTime.now();
String str = dt.format('MM/dd/yyyy hh:mm aa');
dateTime mydate = dateTime.parse(str);

 

Note that we need the format as MM/dd/yyyy and it didnt accept seconds for some reason.

May be someone can shed light on the seconds part.