23

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}"/>
</apex:outputText>

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: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_date.htm and sorry if I am stupid but not so obvious how to do this?

Many thanks.

1

3 Answers 3

21

UPDATE

For the following format

February 2, 2013

please use:

Datetime myDatetime = Datetime.now();
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(dt.day()));
    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

4
  • Thanks suppose you wanted your output to look like February 2, 2013
    – dublintech
    Feb 8, 2013 at 12:14
  • @dublintech just updated & simplified my example Feb 8, 2013 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, 2013 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. DateTime.now().format('yyyyMMdd - HHmmss')
    – Alan Morey
    Feb 8, 2013 at 22:03
19

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

2

http://www.interactiveties.com/b_locale_datetime.php

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

1
  • 1
    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. Feb 8, 2013 at 13:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .