function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jaxdmasterjaxdmaster 

System v/s Date

Hi Guys,

 

We have two classes System and Datetime in apex. Both provides static now  method. I want to know what is difference between now method from System and Datetime class. I would also like to know broad idea of System class and when to use it?

 

 

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
ashish raiashish rai

Hello,

  Well as therotical difference between these two is that datetime.now() returnds time in GMT calaender format i.e. mm/dd/yyyy  hh:mm Period like this 9/22/2011 5:00 AM but when you use System.now() it will retrurns the current datetime of your server in  standared format. This is the theoretical difference but i did't find this practicaly.

All Answers

ashish raiashish rai

Hello,

  Well as therotical difference between these two is that datetime.now() returnds time in GMT calaender format i.e. mm/dd/yyyy  hh:mm Period like this 9/22/2011 5:00 AM but when you use System.now() it will retrurns the current datetime of your server in  standared format. This is the theoretical difference but i did't find this practicaly.

This was selected as the best answer
Afzal MohammadAfzal Mohammad

System.now returns the current date and time in the GMT time zone.

And Datetime.now returns the current Datetime based on a GMT calendar. For example:

datetime myDateTime = datetime.now();
The format of the returned datetime is: 'MM/DD/YYYY HH:MM PERIOD'

 

System class has various utility methods and one should use them while working with any of the following tasks.

 

  • Working with Apex jobs.
  • Programmatically scheduling an apex class.
  • Debugging the code using system.debug statements, etc..
  • Working with approval processes, programmatically.
  • And for various other uses, you may check documentation for more details on how to use system class?

Hope that helps.

 

Afzal

Yahor VolkauYahor Volkau
There are no differences:
System.now() returns the same value as Datetime.now():   2017-01-06 09:34:04
System.today() returns the same date as Date.today():   2017-01-06 00:00:00

But System methods work faster! The difference is quit small, until 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%.