You need to sign in to do that
Don't have an account?
Difference between Two dates in Apex which are in different formats
Hi Friends,
How to find out the difference between two dates in APEX which are in two different formats.
date1=dd/mm/yyyy
date2=mm/dd/yyyy
How to find out the difference between two dates in APEX which are in two different formats.
date1=dd/mm/yyyy
date2=mm/dd/yyyy
Here i have found the article regarding the formats of date in apex:
The format used for dates, times, and names of people in Salesforce is determined by your Locale setting.
The Locale setting affects the format of date, date/time, and number fields, and the calendar. For example, dates in the English (United States) locale display as 06/30/2000 and as 30/06/2000 in the English (United Kingdom) locale. Times in the English (United States) locale display using a twelve-hour clock with AM and PM (for example, 2:00 PM), whereas in the English (United Kingdom) locale, they display using a twenty-four-hour clock (for example, 14:00).
The Locale setting also affects the first and last name order on Name fields for users, leads, and contacts. For example, Bob Johnson in the English (United States) locale displays as Bob Johnson, whereas the Chinese (China) locale displays the name as Johnson Bob.
For Personal Edition users, the locale is set at the organization level via Your Name | Setup | Company Profile | Company Information. For all other users, their personal locale, available at Your Name | Setup | My Personal Information | Personal Information, overrides the organization setting.To find out what date/time format your Locale setting uses:Click Your Name | Setup | My Personal Information | Personal Information.
View the date/time format used in the read-only Created By field. This is the format you should use for entering dates and times in Salesforce fields.
Please mark as best answer if you find this usefull.
1) Convert the date 1 (dd/mm/yyyy) to mm/dd/yyyy format. just parsing it using string methods (split by '/' and then concatenate)
2) Parse both date 1 and date2 strings to date using date.parse method
3) use date.daysBetween() method to find the difference.
List<Account> AllAccounts= new List<Account>([select id,ARR__c,Relationship_Start_Date__c from Account]);
datetime dt = Date.today();
//String st1=dt.format('MM/DD/YYYY');
String utcDt = dt.format('MM/DD/yyyy', 'UTC');
Date dt1 = Date.parse(utcDt);
//system.debug(st1);
for(Account acc:AllAccounts){
Date date2=acc.Relationship_Start_Date__c;
String st2=date2.format();
// Date dt = Datetime.parse(myDate);
Integer noOfDays = dt1.daysBetween(date2);
if(noOfDays<=60){
Opportunity opp = new Opportunity();
opp.CloseDate=date.today().addMonths(2);
opp.Name='test opp1';
opp.StageName='Legal';
//opp.Id='006J000000Hx5Fa';
opp.Amount=acc.ARR__c;
insert opp;
}
}
This is the Code. Is it correct. It is displaying dt1 as 02/57/2015, instead of 02/27/2015