Our Salesforce org has an app event management system storing events with start and end times (Datetime fields). However, the user needs to view all events that occur on a particular date, regardless of when the event starts or ends. To implement this, we need to filter the Datetime field by converting it to Date.
To solve this, I converted the datetime value to the date format using Apex in Salesforce. In this Salesforce tutorial, I will explain how to convert datetime to Date Format in Salesforce Apex using various methods.
Convert DateTime to a Date format in Salesforce Apex
The steps below show the various methods to convert the datetime to a date format in Salesforce Apex.
Convert DateTime to a Date format using the newInstance() method in Salesforce Apex
To convert DateTime to a Date format using the newInstance() method in Salesforce Apex, navigate to the Salesforce developer console, enter the code below in the Apex anonymous window, and execute it.
DateTime dt = Datetime.now();
Date d = Date.newInstance(dt.year(), dt.month(), dt.day());
System.debug('Converted Datetime to date: ' + d);Now, in the execution log’s debug window, we will see that the Datetime is converted to the date format in Apex.

This way, we can convert a DateTime value to Date format in Salesforce Apex using the newInstance() method.
Convert DateTime to a Date format using the Date() method in Salesforce Apex
In Salesforce Apex, another method to get the Date value from Datetime is the Date() method.
Now, open the Salesforce developer console and execute the code below in the Apex anonymous window.
DateTime dt = Datetime.now();
Date d = dt.date();
System.debug('Converted Datetime to date: ' + d);After executing the above apex code, the datetime value will be converted to the date format in Salesforce Apex.

This way, we can convert a DateTime value to Date format in Salesforce Apex using the Date() method.
Convert DateTime to a Date format using the format() method in Salesforce Apex
To convert a DateTime value to a Date format using the format() method in Salesforce Apex, open the Salesforce developer console and execute the code below in the Apex anonymous window.
Datetime dt = Datetime.now();
Date dateOnly = dt.date();
System.debug('formated Date value : ' + dateOnly);In the Apex execution log window, we see that the datetime value is converted to date format in the debug output.

Using the format() method according to the above code structure, we can convert the datetime value to a date in Salesforce Apex.
Convert DateTime to a Date format using the Parse() method in Salesforce Apex
In Salesforce Apex, we can parse the DateTime to a String and then parse the string to a Date value.
To convert a datetime to a date using the parse method, open the Salesforce developer console and execute the code below in the Apex anonymous window.
Datetime dt = Datetime.now();
String dateString = dt.format('yyyy-MM-dd');
Date parsedDate = Date.valueOf(dateString);
System.debug('Parsed Date value : ' + parsedDate);Now, the datetime value will be converted into date format, and we can see that in the execution log window’s debug.

This way, we can convert the datetime value to a date in Salesforce Apex using the parse method.
Conclusion
In this Salesforce tutorial, we have learned to convert the datetime to a date value in Salesforce Apex. We have discussed the various methods to convert the datetime value to date format, such as Date.newInstance(), Date(), format(), and Parse() methods.
Using the above methods, you can efficiently convert the datetime value to the date format in your Salesforce Apex environment.
You may also like to read.
- Convert Date to String in Salesforce Apex
- Convert String to Datetime in Salesforce Apex
- Convert String to Date in Salesforce Apex
- Convert Unix Timestamp to DateTime using Apex in Salesforce
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.