Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In a visual force page, I can take a date and do this...

<apex:outputText value="{0,date, MMMM d,  yyyy}">
    <apex:param value="{!relatedTo.my_date__c}"/>

I want to be able to do that in Apex code. So I want a method which takes a date and returns a certain format as a string.

I check the API: and sorry if I am stupid but not so obvious how to do this?

Many thanks.

share|improve this question
Try Datetime.format instead of Date.format.… – Brad Ullery Feb 8 '13 at 12:15
up vote 16 down vote accepted


For the following format

February 2, 2013

please use:

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

Im using the following functions for the timestamp:

// This method returns a whole timestamp as a string
public String timeStamp(){
    Datetime dt = Datetime.NOW();
    String day = checkLength(String.valueOf(;
    String month = checkLength(String.valueOf(dt.month()));
    String hour = checkLength(String.valueOf(dt.hour()));
    String minute = checkLength(String.valueOf(dt.minute()));
    String second = checkLength(String.valueOf(dt.second()));

    return = String.valueOf(dt.year()) + month + day + '-' + hour + minute + second;

// Here i check the length of the day/month/...
// because the month() for example can return 3 for March and i want to have 03
private String checkLength(String val){
    if(String.isNotBlank(val) && val.length() == 1){
        val = '0' + val;
    return val;

And my output looks like this then: 2013.02.08 08:30:56

share|improve this answer
Thanks suppose you wanted your output to look like February 2, 2013 – dublintech Feb 8 '13 at 12:14
@dublintech just updated & simplified my example – Sergey Utko Feb 8 '13 at 12:26
Your timeStamp() method seems very inefficient -- how could you refactor it to just use the Datetime.format() method and still get the same output? Post that and I'll remove my downvote. – tomlogic Feb 8 '13 at 16:42
@mast0r I agree with @tomlogic. Your implementation for timeStamp() is very inefficient and is a bad example. It would be much more efficient to use the DateTime.format() e.g.'yyyyMMdd - HHmmss') – Alan Morey Feb 8 '13 at 22:03

You are looking for the Datetime.format() method. In your instance, you should be able to use:

String formattedDate = relatedTo.my_date__c.format('MMMM d, yyyy');

This method utilizes the same format as the Java SimpleDateFormat. Specifically, you should read up on the Date & Time Patterns section and review the table there. It will allow you to format the date however you like. The following strings are available for formatting:

Examples of available Strings

The following are some examples of the strings you can use and what type of results would be returned:

Examples of strings in use

share|improve this answer

The component that is explained is very good for formatting.You may like to use that with specific locale

share|improve this answer
This is a cool article but it doesn't really answer the user's question. He is trying to change the formatting in Apex, not in Visualforce. – Jesse Altman Feb 8 '13 at 13:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.