+ Start a Discussion
sfdc dev 2264sfdc dev 2264 

System.StringException: Starting position out of bounds: 11 Help needed

Hi,

I have a controller which splits the date/time from a text field to a date/time field, Its working fine, But i am getting the below apex exception

caused by: System.StringException: Starting position out of bounds: 11
Class.casedeparturedatecalculation.datecalculation: line 29, column 1

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

public class casedeparturedatecalculation {
    
    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;
                }
            
            }
        }
        
    }
    
}

Kindly help me in the same.


Thanks in Advance


 
Adilson Arcoverde JrAdilson Arcoverde Jr
Hi sfdc dev 2264,

Could you send some Departure_Date_Time__c values? My first guessing is that you are storing wrong date/time string into this field.

Usually date/time format follows the pattern: yyyy-MM-ddThh:mm:ss.SSSZ. To use Datetime.valueOf(String input) you could use the pattern: yyyy-MM-dd hh:mm:ss.

For the second pattern, I would use the following code to get a date value:


    public static Date datecalculation(String departureTime ) {
        Datetime dt = Datetime.valueOf(departureTime);                                 
        Date myDate = Date.valueOf( dt.format('yyyy-MM-dd') );
        System.debug(myDate);        
        return myDate;
    }


I have used 2018-12-01 10:00:00 as input string and it worked fine to me.

Your code is seems to get, for instance, day from the beginning of the string. 

Please refer to https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html for full date/time patterns.

Best regards.
Rishab TyagiRishab Tyagi
Hello sfdc dev 2264,

After reading through your code I think that your problem lies with some bad data. Looking at the current scenario I know that your Departure_Date_Time__c field is a text type field, and you are storing date/time values in it. However, I believe that some values in the field might not have the time part, making the string of the length 11(0-10) characters. That's where the problem lies, as substring cannot work on an index that does not exist. eg. let's say that one of the Departure_Date_Time__c value is '27/12/2018' instead of '27/12/2018 08:00AM'. Now, in this case, the departure date will be parsed but we don't have time to parse. My suggestion, for this scenario would be to add an 'if else' statement checking the length of the field before parsing. If the length is 20 characters, or whatever the actual calculated length would be then parse the time part, else leave the time part as 00.

Also for storing date/time values, you must look at the Date/Time field instead of a Text field as the Date/Time functions help a lot getting individual values without going through the pain of substring.