How to get Date from DateTime field using Apex in Salesforce?

How to get Date from DateTime field using Apex in Salesforce?

Date() can be used to get the date from datetime field.

Sample Code:

Datetime LastModifiedDateTime = Datetime.now(); 
System.debug( 'LastModifiedDateTime is ' + LastModifiedDateTime ); 
Date ModifiedDate = LastModifiedDateTime.Date();
System.debug( 'ModifiedDate is ' + String.valueOf( ModifiedDate ) );

Output:

Leave a Reply