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
JamesSSJamesSS 

How to get today date in apex class?

My input:

System.debug('Date.today();************ ' + date.today());

 

Output:

Date.today();************  2013-06-07 00:00:00

 

expected output:

Date.today();************  2013-06-07 

 

Is possible to get my expected output?

Best Answer chosen by Admin (Salesforce Developers) 
sandeep@Salesforcesandeep@Salesforce

System.debug('************ ' + system.today().format());
System.debug('************ ' + date.today().format());

 

Both will work like 

 

************ 07/06/2013

************ 07/06/2013

All Answers

Gunish Rai ChawlaGunish Rai Chawla
You would have to use, Date Methods to extract, Month, Year and Date, and re-concatenate all of these to form a string.
JamesSSJamesSS

Thanks Gunish.

Please provide some example for that

sandeep@Salesforcesandeep@Salesforce

System.debug('************ ' + system.today().format());
System.debug('************ ' + date.today().format());

 

Both will work like 

 

************ 07/06/2013

************ 07/06/2013

This was selected as the best answer
Navaprasad Reddy AndeNavaprasad Reddy Ande
Please try this

DateTime todaysDate = System.today();
String todaysDateStr = todaysDate.format('yyyy-MM-dd');
System.debug(todaysDateStr);

Please mark this as best answer if this works for you.
 
SKMuklesh AliSKMuklesh Ali
By default it's show date and time format.
use format method show only date.
Example:
System.debug(date.today().format());
 
Richard Fiekowsky 3Richard Fiekowsky 3
Note that 
date.today().format()
works, but it cannot take a format string :  
date.today().format('yyyy-mm-dd') 
causes "Method does not exist or incorrect signature: void format(String) from the type Date" .