How to Convert DateTime to Date in Apex Salesforce

by Rijwan Mohmmed
2 comments
How to Convert DateTime to Date in Apex Salesforce

There are two methods to get date from DateTime

  1. By Date Instance : Here we will get date by Date.newInstance() method
Public Class DatetimetoDateConversion{
       public static void dateTimeToDate(){
              DateTime dtime = System.now();
              Date d = Date.newInstance(dtime.year(), dtime.month(), dtime.day());
              System.debug('Date==>' + d);
       }
}

2. By Date Method: Here will get the Date by Apex Date() method

Public Class DatetimetoDateConversion{
       public static void dateTimeToDate(){
              DateTime dtime = System.now();
              Date d = dt.date();
              System.debug('Date==>' + d);
       }
}
What’s your Reaction?
+1
0
+1
0
+1
0
+1
1
+1
0
+1
3

You may also like

2 comments

Tarah November 8, 2021 - 8:44 am

I used to be recommended this web site by my cousin. I am now not positive whether
this post is written through him as nobody else know such particular approximately my
problem. You are wonderful! Thanks!

Reply
Deltax November 8, 2021 - 8:56 am

Thanks @Tarah keep reading

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.