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
sfdx0007sfdx0007 

System.StringException: Starting position out of bounds: 11 exception help needed

Hi All,

I am getting the below exception 

System.StringException: Starting position out of bounds: 11 Class.Test.datecalculation: line 29

Please help me the issue here
public class test {
    
    public static void datecalculation(List<Case> caseList )
    {
        if(caseList.size() >0 )
        {
            List<Case> caseList2  = new List<Case>();
            for(Case c: caseList)
            {
                if(c.Departure_Date_Time__c!=null){
                //Case ca = new Case();
                String firstDate = c.Departure_Date_Time__c;
                String month = firstDate.substring(3,5);
                String day = firstDate.substring(0,2);
                String year = firstDate.substring(6,10);          
                string hour = firstDate.substring(11,13);               
                string minute = firstDate.substring(14,16);               
                string second = firstDate.substring(17,19);
                  
                string stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' 
                    + minute +  ':' + second;
                System.debug('stringDate:'+stringDate);
                
                Date myDate = Date.valueOf(stringDate);
                c.Updated_Departure_Date_Time__c = myDate;
                }
            
            }
        }
        
    }
    
}

ERROR LINE :
                string hour = firstDate.substring(11,13);               

Kindly help me on the same,

Thanks in advance
​​​​​​​
Best Answer chosen by sfdx0007
Anthony McDougaldAnthony McDougald
Hello sfdx,
Hope that your day is off to an amazing start. You're getting this error because the starting index doesn't exist, blank space, or null. Please try any of the code below and hope this helps. May God bless you abundantly.
string hour = firstDate.substring(10,13);
OR
string hour = firstDate.substring(12,14);


Best Regards,
Anthony McDougald

All Answers

Anthony McDougaldAnthony McDougald
Hello sfdx,
Hope that your day is off to an amazing start. You're getting this error because the starting index doesn't exist, blank space, or null. Please try any of the code below and hope this helps. May God bless you abundantly.
string hour = firstDate.substring(10,13);
OR
string hour = firstDate.substring(12,14);


Best Regards,
Anthony McDougald
This was selected as the best answer
sfdx0007sfdx0007
Thanks Anthony , I will check the same, Thanks so much :)