0

I have a requirement to convert the value of a map to to specific date

trigger SPE2updateVintage on SPE_SPETracker__c (after Insert) {
Public Set<id> speTrackerId = new Set<id>();
public list<SPE_SPETracker__c> liSPESPETracker = new List<SPE_SPETracker__c>();
public map<String,Integer> mapDate = new map<string, Integer> {
'january' => 01,
'february' => 02,
'march' => 03,
'April' => 04,
'May' => 05,
'June' => 06,
'July' => 07,
'August' => 08,
'September' => 09,
'October' => 10,
'November' => 11,
'December' => 12 
};

public map<id, list<String> > mplanwithTrack =new map<id, list<String> > ();

    for( SPE_SPETracker__c Tracker : Trigger.new)
    {
        speTrackerId.add(Tracker.SPEPlan__c);  


    }
    liSPESPETracker =[select id ,name,DateOfExecution__c,SPEPlan__c from SPE_SPETracker__c where SPEPlan__c  in :speTrackerId ];

    for(SPE_SPETracker__c speTrackerList :liSPESPETracker){
  if(!mplanwithTrack.containskey(speTrackerList.SPEPlan__c)){
    mplanwithTrack.put(speTrackerList.SPEPlan__c,new list<String>{});
  }
  mplanwithTrack.get(speTrackerList.SPEPlan__c).add(speTrackerList.name);

}
 system.debug('&&&Test&&&'+mplanwithTrack);

    //String strTest = 'December-2015';
    //String[] arrTest =  mplanwithTrack.get(speTrackerId[0]).split('\\-');
    //system.debug('&&&Test123'+arrTest);

in the above code I am getting the name as a string and that contains Month name and year in this format January - 2016 and that I need to convert to a specific date like dd/mm/yyyy date can be populated as a hard code value . can anyone please help me how to trim that string to a date.

2
  • 1
    What day should be in the resulting date? Are you talking about creating a Date instance? Where do you want to put that instance?
    – Keith C
    Commented Mar 7, 2016 at 8:33
  • @KeithC any date like 01/01/2016 something like and . This is not a date instance . you can see the Map is ID and String . one key will be having multiple string in above mention format(Month name and year) and I need to convert them into date then I need to take the oldest date to populate in parent object record.
    – subrat
    Commented Mar 7, 2016 at 9:12

1 Answer 1

0

Try this:

map<String,Integer> mapDate = new map<string, Integer> {
'january' => 01,
'february' => 02,
'march' => 03,
'April' => 04,
'May' => 05,
'June' => 06,
'July' => 07,
'August' => 08,
'September' => 09,
'October' => 10,
'November' => 11,
'December' => 12 
};

String s = 'December-2015';
String mn = s.split('-')[0]; 
integer y = Integer.valueOf(s.split('-')[1]); 
integer m = mapDate.get(mn);
Date d  = date.newInstance(y,m,1);
System.debug('Date = ' +d);

Note: i am assuming that you will always get input in 'MonthName - Year' format. if that not true then you should add some error handling in your code.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .