System vs Date/DateTime class

Is there any difference between System.today() and Date.today()?

Similarly, is there any difference between System.now() and DateTime.now()?

Answer

They give you the same data. Their descriptions are almost verbatim as well:

System.now()
Returns the current date and time in the GMT time zone.

Datetime.now()
Returns the current Datetime based on a GMT calendar.

And for today they are indeed verbatim:

System.today()
Returns the current date in the current user’s time zone.

Date.today()
Returns the current date in the current user’s time zone.

If you care about character count, Date.today() is more efficient than using System, but System.now() is more efficient than using Datetime. Either way, it’s a two character difference.


There is actually a performance difference as well. Note that even slower executions will take less than 5 microseconds, so this difference is not likely to affect your code performance on the whole unless you are calling these methods millions of times.

  • For Date.today(), the cost per invocation is about 4.43µs, whereas System.today() costs about 3.85µs per invocation, a difference of about ~15%.
  • For Datetime.now(), the cost per invocation is about 3.48µs, whereas System.now() costs about 2.97µs per invocation, a difference of ~17%.

Attribution
Source : Link , Question Author : Fernando Gavinho , Answer Author : Community

Leave a Comment