2

I have two Date values. I need to find number of weeks between them. I want to do it Apex Class. Not looking for formula fields.

So far I tried:

Integer numberofweeks = (projectStartDate - opp.Project_Start_Date__c)/7; 

I receive this error:

Date arithmetic expressions must use Integer or Long arguments

1
  • 1
    When you have information to add, please edit your post to include it instead of burying it in comments.
    – Adrian Larson
    Apr 26, 2016 at 14:44

3 Answers 3

3

You need the Date.daysBetween method to get the difference between two Date values. The following works for me:

Date today = Date.today();
Date nextWeek = Date.today().addDays(7);
system.assertEquals(1, today.daysBetween(nextWeek)/7);
2
  • thanks. it worked. my stupid mind didn't think this logic.
    – Razu
    Apr 26, 2016 at 21:01
  • @Razu Not stupid, but just remember the documentation is really helpful sometimes!
    – Adrian Larson
    Apr 26, 2016 at 21:03
1

Here it is an example:

ApexTest:

@isTest
private class DateExampleTest {

    @isTest static void test_method_one() {
        //test 7 days diff
        Date startDate = Date.today().addDays(0);
        Date endDate = Date.today().addDays(7);

        Integer diffDays = startDate.daysBetween(endDate);

        System.debug(diffDays);
        System.assertEquals(7, diffDays);

        Integer diffWeeks = diffDays / 7;

        System.debug(diffWeeks);
        System.assertEquals(1, diffWeeks);

        //test 15 days diff
        startDate = Date.today().addDays(0);
        endDate = Date.today().addDays(15);

        diffDays = startDate.daysBetween(endDate);

        System.debug(diffDays);
        System.assertEquals(15, diffDays);

        diffWeeks = diffDays / 7;

        System.debug(diffWeeks);
        System.assertEquals(2, diffWeeks);

        //test 14 days diff back and forward
        startDate = Date.today().addDays(-7);
        endDate = Date.today().addDays(7);

        diffDays = startDate.daysBetween(endDate);

        System.debug(diffDays);
        System.assertEquals(14, diffDays);

        diffWeeks = diffDays / 7;

        System.debug(diffWeeks);
        System.assertEquals(2, diffWeeks);

    }

}
0

My initial approach would be to create a new formula field called "Number of Weeks " which would have the value of (date2-date1)/7

In the apex class you'd have something like

Integer x = date1.daysBetween(date2) /7;
6
  • I want to do it in Apex class
    – Razu
    Apr 26, 2016 at 14:29
  • 1
    post first your current apex code and I'll help you modify it Apr 26, 2016 at 14:30
  • Integer numberofweeks = (projectStartDate - opp.Project_Start_Date__c)/7; it is giving error.
    – Razu
    Apr 26, 2016 at 14:31
  • can you psot the error as well? Apr 26, 2016 at 14:34
  • error : Date arithmetic expressions must use Integer or Long arguments
    – Razu
    Apr 26, 2016 at 14:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .