1

I am new to the salesforce and trying to do the following

System.debug('Date :'+ Date.today());
System.debug('Date + 1 :'+Date.today().addDays(2));
System.debug('Add Months : '+Date.today().addMonths(3));
System.debug('Add Years : '+Date.today().addYears(5));

System.debug('Day Of year : '+Date.dayOfYear()+'');

Please refer: https://stackoverflow.com/questions/43375201/non-static-method-cannot-be-referenced-from-a-static-context-integer-date-dayof#

6
  • 3
    Date is the class name and not an instance. Date.today() creates an instance so e.g. Date.today().dayOfYear() will compile and work. Suggest you Google on the subject of instance methods and static methods.
    – Keith C
    Apr 12 '17 at 17:18
  • First Of all, I doubt if you can use Date.today(). I think system.today() will give the expected output. And try using System.today().dayOfYear() [Replace Date with System and try] Apr 12 '17 at 17:35
  • 1
    @MuzammilBajaria - Date.today() is perfectly valid
    – Eric
    Apr 13 '17 at 0:55
  • Did you happen to mistakenly create a class named Date? if so delete it
    – Eric
    Apr 13 '17 at 0:57
  • 1
    @MuzammilBajaria System vs Date/DateTime class
    – martin
    Apr 13 '17 at 2:14
3

You are trying to call an instance method as if it were static. That won't work. Here's an analogous example for you:

public class MyClass
{
    public void instanceMethod()
    {
        // do stuff
    }
    public static void staticMethod()
    {
        // do stuff
    }
}

You can't call MyClass.instanceMethod() nor new MyClass().staticMethod(). You're trying to do the former.

1

This will work:'

System.debug('Day Of year : '+Date.today().dayOfYear());

Result Gives: 103, as today's date is 13th-Apr-2017.

1

Technical Explanation

  • Static methods can be used directly with a class. ex-> Date.today()

  • Non-static methods can't be used directly. ex -> Date.year() - error or Date.dayOfYear() - error .So the solution for this problem is to create an instance of the class which can be done as - Class className = new Class(); className.nonstaticMethod();

  • However, in this scenario, you can't create an instance for Date because for some classes you just can't create an instance in general. But they have already provided some methods where you can create an instance of the Date class. example - date.newInstance(year, month, day)

  • So in this case you can use something like --> --> Date.newInstance(year, month, day).dayOfYear(); // to use the date of your choice or, --> Date.today().dayOfYear(); // to use today's date

As the above examples, you can use any non-static methods with the date instance.

General explanation - So in this particular case, when you use date.year() or date.dayOfyear(), the system doesn't actually know what to return because you aren't giving it a proper date value. So basically, If you don't give it a date value, It doesn't make sense to ask about the year or about the day of the year, right? So this is kind of general way I came up with to understand this problem, If it makes sense to you. When you are using date.today(), so it doesn't need a particular date value. Today will always return the value of that particular day.

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.