Skip to Content

TheBlogReaders.com

Salesforce.com, PHP, MySQL, Javascript, Ajax, Htacces

How to change date formats using Apex Class?

Be First!

How to change date formats using Apex Class?

For Example 1:
2017-01-24 to change the date format to be in DD.MM.YYYY

Below example should work if you can use a string as the final output, otherwise dates are always displayed in the local context of the user.

Date d = date.today();
String dt = DateTime.newInstance(d.year(),d.month(),d.day()).format('d-MM-YYYY');
system.debug(logginglevel.error,dt);

For Example 2:
2017-01-24 00:00:00 to  01/24/2017 (mm/dd/yyyy)

This is already have a date ‘as a date’, but not as a string. The format about (2017-01-24 00:00:00) is just a visual representation of a value contained in variable of a DateTime type. If its want the date to appear in the specified format (01/24/2017), just output it using format() method:

Datetime yourDate = Datetime.now();
String dateOutput = yourDate.format('dd/MM/yyyy');

For Example 3:
January 24, 2017

Datetime myDatetime = Datetime.now();
String myDatetimeStr = myDatetime.format('MMMM d,  yyyy');

(1)

Previous
Next

Leave a Reply