How to format date to 'yyyy-MM-dd' using Apex

4.5K    Asked by AnishaDalal in Devops , Asked on May 27, 2024

 I'm building dynamic SOQL query for the search page I have the following code: VFP:

    
Apex:
My Invoice_Date__c is a date field in the object
If I execute the following code in Anonymous window I do see the correct format but with my building dynamic SOQL I see the 2017-07-17 00:00:00
Date d = s.Invoice_Date__c; string dateStr = DateTime.newInstance(d.year(),d.month(),d.day()).format('yyyy-MM-dd'); system.debug('//' + dateStr);
Dynamic query building:
if (s.Invoice_Date__c != null) { Datetime d = s.Invoice_Date__c; String dateOutput = d.format('yyyy-MM-dd'); string invoiceDateFormated = DateTime.newInstance(d.year(),d.month(),d.day()+1).format('yyyy-MM-dd'); system.debug('dateOutput: ' + dateOutput); query += ' and Invoice_Date__c = '+invoiceDateFormated ; }

other thing I have noticed that even if I select today's date which is 07/17/2017 but in debug it showing me 07/16/2017 that's why I end-up adding +1 What is salaes force dateformat?

Answered by Carl Paige

You didn't do the same thing in both examples:

      String foo(Date input) { // your first example return DateTime.newInstance( input.year(), input.month(), input.day() ).format('yyyy-MM-dd'); } String bar(Date input) { // your second example Datetime output = input; // Type Coercion ^^^^^ return output.format('yyyy-MM-dd'); } String baz(Date input) { // another approach return DateTime.newInstance( input, Time.newInstance(0,0,0,0) ).format('yyyy-MM-dd'); } system.debug(foo(Date.today())); system.debug(bar(Date.today())); system.debug(baz(Date.today()));

In your first example, you use foo, but in your second, you use bar. What you do in bar is called Type Coercion where you assign a Date to a Datetime. That gives it a Time instance of (0,0,0,0), but a Time Zone of GMT. So you could also fix bar by using formatGmt:

String qux(Date input) { // your second example rewritten Datetime output = input; return output.formatGmt('yyyy-MM-dd'); // ^^^ }
You could also think about your original bar as being equivalent to a slight change in baz:
String quux(Date input) { Datetime output = Datetime.newInstanceGmt(input, Time.newInstance(0,0,0,0)); // now output is the same as it was in `bar` return output.format('yyyy-MM-dd'); }
You have the Datetime in the GMT Timezone, so you need to use formatGmt:
String garply(Date input) { Datetime output = Datetime.newInstanceGmt(input, Time.newInstance(0,0,0,0)); // now output is the same as it was in `bar` return output.formatGmt('yyyy-MM-dd'); // ^^^ }

Salesforce date format followed by its field is YYYY/MM/DD, and following variations that include a time stamp as well: YYYY-MM-DD. YYYY-MM-DD hh:mm:ss.



Your Answer

Answer (1)

In Apex, you can format a date to the 'YYYY-MM-DD' format using the format() method available in the Date class. Here's how you can do it:


Date myDate = Date.today(); // Assuming you have a date object
String formattedDate = myDate.format('yyyy-MM-dd');
System.debug(formattedDate); // Output: YYYY-MM-DD formatted date

This code will output the date in the desired format. The format() method allows you to specify a formatting pattern using placeholders like 'yyyy' for the year, 'MM' for the month, and 'dd' for the day.

Ensure that you replace myDate with your actual Date object or variable containing the date you want to format.

If you have a DateTime object instead of just a Date, you can use the same format() method:

DateTime myDateTime = DateTime.now(); // Assuming you have a DateTime object
String formattedDateTime = myDateTime.format('yyyy-MM-dd');
System.debug(formattedDateTime); // Output: YYYY-MM-DD formatted date

This will format the date part of the DateTime object to 'YYYY-MM-DD'.


6 Months

Interviews

Parent Categories