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.
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.Date.today()
is perfectly validDate
? if so delete it