1

below is my code n I am getting date formatting issue. I am gettin DEBUG|*reqDate*2016-07-29 00:00:00 but i want it in 2016-07-29T08:00:04.000+0000 format. How can i do it

Decimal noOfDays;
        String convDate;
        CS_Custom_Settings__c cs = CS_Custom_Settings__c.getOrgDefaults();
        if(cs != null && cs.Number_of_days_Purge_Batch_Job__c != null){
            noOfDays = cs.Number_of_days_Purge_Batch_Job__c;
        }
        Date reqDate = Date.today() - Integer.valueOf(noOfDays);
        System.debug('*reqDate*'+reqDate);
        for(Purge_Batch_Job__c batchJob : Purge_Batch_Job__c.getAll().values()){
            objAPINames.add(batchJob.Object_API_Name__c);   
        }
        for(String objName : objAPINames){
            //System.debug('Query : ' + 'Select Id from ' + objName + ' where CreatedDate <= ' + reqDate);
            System.debug('Query : ' + 'Select Id from ' + objName + ' where CreatedDate <=: reqDate');
            for(sObject s : Database.Query('Select Id from ' + objName + ' where CreatedDate <=: reqDate')){ 
                recordIds.add((Id)s.Id);
            }
  • If you need an timestamp with reqDate, reqDate should have been DateTime instead of Date.This should been DateTime reqDate = System.now() - Integer.valueOf(noOfDays);. May i know,why do you want 2016-07-29T08:00:04.000+0000 this format? Is it for adding it in where clause? – Praveen Jul 29 '16 at 9:01
  • what do you want to do with formatted date ? – Ratan Paul Jul 29 '16 at 9:05
  • This doesn't sound like a display format issue as much as an issue with the value that your variable holds. Date variables don't have a time component. There is no 8 hours and 4 minutes associated with reqDate. – martin Jul 29 '16 at 9:32
1

Consider using Date Literals (documentation) instead:

Integer days = (Integer)cs.Number_of_days_Purge_Batch_Job__c;
String soql = 'Select Id from ' + objName + ' where CreatedDate <= LAST_N_DAYS:' + days;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.