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#

  • 2
    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] – Muzammil Bajaria 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
2

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.

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.