I came across a weird scenario. The format method is replacing month with '00' in its result.
A portion of my code: (Executed Anonymously to test)
System.Debug(logginglevel.error,'****m.date_approved__c***'+m.date_approved__c);
if(m.date_approved__c!=null) {
System.Debug(logginglevel.error,'****year***'+m.date_approved__c.year());
System.Debug(logginglevel.error,'****month***'+m.date_approved__c.month());
System.Debug(logginglevel.error,'****day***'+m.date_approved__c.day());
Datetime dt = DateTime.newInstance(m.date_approved__c.year(), m.date_approved__c.month(),m.date_approved__c.day());
System.Debug(logginglevel.error,'****date before formatting***'+dt);
approveddate = dt.Format('mm/dd/yyyy');
}
System.Debug(logginglevel.error,'****approveddate***'+approveddate);`
Debug Results
11:24:31.35 (85764301)|USER_DEBUG|[138]|ERROR|****m.date_approved__c***2016-06-24 00:00:00
11:24:31.35 (85826327)|USER_DEBUG|[140]|ERROR|****year***2016
11:24:31.35 (85873389)|USER_DEBUG|[141]|ERROR|****month***6
11:24:31.35 (85950679)|USER_DEBUG|[142]|ERROR|****day***24
11:24:31.35 (86109856)|USER_DEBUG|[144]|ERROR|****date before formatting***2016-06-24 07:00:00
11:24:31.35 (86188235)|USER_DEBUG|[149]|ERROR|****approveddate***00/24/2016
I tested this method separately, and it is working fine.
MyObj__c m = [select id, date_approved__c from MyObj__C WHERE Id = 'a033900000EU9LN'];
Datetime dt = DateTime.newInstance(m.date_approved__c.year(), m.date_approved__c.month(),m.date_approved__c.day());
System.Debug(logginglevel.error,'****date before formatting***'+dt);
System.Debug(logginglevel.error,'*****testing format method******'+dt.Format('MM/dd/yyyy'));`
Debug result:
11:27:41.1 (6791165)|USER_DEBUG|[3]|ERROR|****date before formatting***2016-06-24 07:00:00
11:27:41.1 (6886491)|USER_DEBUG|[4]|ERROR|*****testing format method******06/24/2016
I used the same record in both the cases
Any thoughts?