+ Start a Discussion
Ben Merton 15Ben Merton 15 

Add hours to date time using the addDays method...

I am trying to add days to an existing date variable as per the following:
 
public class CreateIndentController{

    decimal cycletime;
    decimal sequenceno;
    date completiondate;
    date requireddate;

    public CreateIndentController()
    {
        productId = ApexPages.currentPage().getParameters().get('unifize_Completion_Date__c');
       
        List<unifize__BOM_Item__c> cycletimesequencelist = [select unifize__Cycle_Time__c, unifize__Sequence_No__c FROM unifize__BOM_Item__c WHERE unifize__Product__c = :productId];
        cycletime=cycletimesequencelist[0].unifize__Cycle_Time__c;
        sequenceno=cycletimesequencelist[0].unifize__Sequence_No__c;
    
    }
    public pageReference onLoad()
    {
       
   
        List<unifize__BOM_Item__c> bomItems = [select unifize__Quantity__c, unifize__GSI_No__c, unifize__Quantity_UOM__c from unifize__BOM_Item__c where unifize__Product__c = :productId];

         
                List<unifize__Material_Request__c> indents = new List<unifize__Material_Request__c>();
        for (unifize__BOM_Item__c item : bomItems)
        {
            indent.unifize__Required_Date__c=completiondate.addDays(-1*cycletime/24);
            indents.add(indent);
        }
        if (indents.size() > 0)
        {
            insert indents;
        }
        return new PageReference('/' + workOrderId);
    }
}
I am having a problem with the following line:

 indent.Required_Date__c=completiondate.addDays(-1*cycletime/24);

It is returning this error 
Error: Compile Error: Method does not exist or incorrect signature: [Date].addDays(Decimal) at line 60 column 46

Can you help?
 
Best Answer chosen by Ben Merton 15
srlawr uksrlawr uk
The error is quite right, there is no "addDays" method that takes a decimal. Only one that takes an Integer.

You can't add "hours" to a date field, as it has no concept of time, only dates. You will need to use a DateTime field type to work with hours/time, and then there is an addHours method (which also takes an integer).

If you want to add whole days to your field, but do so with a mathematical function that returns a type Decimal (but you know it will be an integer result, or are happy with standard rounding) you can use something Integer.ValueOf() like this:
indent.Required_Date__c=completiondate.addDays(Integer.valueOf(-1*cycletime/24));