I am getting date as a string '03-Sep-24' from an external system. What is the easiest way to parse it and save it in a date field?
1 Answer
What i did was to manually create a map of month and their number values and create a string which apex can parse. Any better answer is appreciated.
Public static Date convertStringToDate(String value){
Map<String,String> monthNumberConversionMap=new Map<String,String>{'JAN'=>'1','FEB'=>'2','MAR'=>'3','APR'=>'4','MAY'=>'5','JUN'=>'6','JUL'=>'7','AUG'=>'8','SEP'=>'9','OCT'=>'10','NOV'=>'11','DEC'=>'12'};
Date valueToReturn=null;
if(!String.IsBlanK(value)){
try{
String[] splitstr=value.split('-');
String dd=splitstr[0];
String mmm=splitstr[1];
String yy=splitstr[2];
valueToReturn=Date.valueOf('20'+yy+'-'+monthNumberConversionMap.get(mmm.toUpperCase())+'-'+dd);
}
catch(Exception e){
System.debug('Exception '+e);
throw e;
}
}
return valueToReturn;
}