+ Start a Discussion
SFDC pvSFDC pv 

In API response am getting date format like 30-Sep-2018. I want to convert this date to dd/mm/yyyy format

                              string ffdate = program.ffExpireDate;  
                              system.debug('ffexpirydate'+ffdate);
                               date dt = date.parse(ffdate);
                               FF.Expiry_Date__c = dt; 
Getting Error :-   |FATAL_ERROR|System.TypeException: Invalid date: 30-Sep-2018

Log: 
|USER_DEBUG|[49]|DEBUG|ffexpirydate30-Sep-2018 
|FATAL_ERROR|System.TypeException: Invalid date: 30-Sep-2018
                               
Best Answer chosen by SFDC pv
Naveen IlaNaveen Ila
Define base Map as below or use custom setting 

Map<String, Integer> monthAndMonthNumberMap = new Map<String, Integer>();
monthAndMonthNumberMap.put('Jan', 1);
monthAndMonthNumberMap.put('Feb', 2);
monthAndMonthNumberMap.put('Mar', 3);

Code Snipet to get date :

String myDateStr = '30-Mar-2018';
List<String> myDateList = myDateStr.split('-');
Integer yearVal = Integer.valueOf(myDateList.get(2));
Integer monthVal = monthAndMonthNumberMap.get(myDateList.get(1));
Integer dayVal = Integer.valueOf(myDateList.get(0));
date mydate = date.newinstance(yearVal, monthVal, dayVal);
System.debug('$$$ mydate: '+ mydate);

All Answers

devedeve
Hello,

Please try this to convert date in dd/mm/yyyy format:-

Date d = date.today();
String dt = DateTime.newInstance(d.year(),d.month(),d.day()).format('dd-MM-YYYY');
Naveen IlaNaveen Ila
Define base Map as below or use custom setting 

Map<String, Integer> monthAndMonthNumberMap = new Map<String, Integer>();
monthAndMonthNumberMap.put('Jan', 1);
monthAndMonthNumberMap.put('Feb', 2);
monthAndMonthNumberMap.put('Mar', 3);

Code Snipet to get date :

String myDateStr = '30-Mar-2018';
List<String> myDateList = myDateStr.split('-');
Integer yearVal = Integer.valueOf(myDateList.get(2));
Integer monthVal = monthAndMonthNumberMap.get(myDateList.get(1));
Integer dayVal = Integer.valueOf(myDateList.get(0));
date mydate = date.newinstance(yearVal, monthVal, dayVal);
System.debug('$$$ mydate: '+ mydate);
This was selected as the best answer
Naveen IlaNaveen Ila
I am getting below value, when I am executing shared code:

User-added image

Can you share your code snipeet once. 
Naveen IlaNaveen Ila
Can you make sure, you are correctly passing values to the funcation date.newinstance(yearVal, monthVal, dayVal);
Naveen IlaNaveen Ila
Can you share Component Event code 

Also handler controller method, which is getting called upon firing this event. 
SFDC pvSFDC pv
Hi @Naveen,

Thanks. My bad i wrongly passed the values.
Naveen IlaNaveen Ila
Can you mark my answer as best one. Thanks in advance.