3

This is a generic question

apex:outputText value="{**0**,date,MM'/'dd'/'yyyy}"

What I wanted to know is what is the 0 in value used for?

1

Here, 0 tells you which argument index to merge in. So usually, you just have one argument, and merging it in uses the 0-index parameter. But you might for some reason have multiple arguments (each represented by an <apex:param>).

<apex:outputText value="{0} {1, date, MM}">
    <apex:param value="test" />
    <apex:param value="{!NOW()}" />
</apex:outputText>

As represented by the above example, you can merge multiple arguments into one format string. It is very similar behavior to the String.format method in Apex, though I don't believe you can do date formatting that way.

1

The value attribute on "apex:outputText" is a valid format. It must be a positive number, and of type Number, Date, Time, or Choice.

For more info please check this url

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_outputText.htm?search_text=apex:outputtext

As per String.formate in Java and apex you can say.....

under documented Apex String API i.e.

String formattedString = String.format(String template, List<String> arguments)

String.format(..) is again based on Java MessageFormat class, its documentation says :

Treat the current string as a pattern that should be used for substitution in the same manner as apex:outputText.

Nice part of this String API is that it supports basic text substitution for ex.

// Create a Template String, that has tokens of form {index} 

String templateString = 'Hello {0}, Good to see you in {1}';

// String argument list or array matching each {index} in Template String.

String[] arguments = new String[] {'Ram' , 'India'};

// Call String.format() to get the token replaced

String formattedString = String.format(templateString, arguments);

System.debug(formattedString);

// output : Hello Ram, Good to see you in India

Bad part about this String API is it only supports String arguments for token replacement. Though both Java’s MessageFormat and Visuaforce apex:outputText tag accept non-string arguments like Date, Datetime & Currency and are capable of doing very smart formatting with them. For ex. in visualforce one can format date using outputText as follows.

<apex:outputText value="The formatted time right now is: 
    {0,date,yyyy.MM.dd G 'at' HH:mm:ss z}">
   <apex:param value="{!NOW()}" />
</apex:outputText>

output: The formatted time right now is: 2004.11.20 AD at 23:49:02 GMT

Similar Java example with more options would be

Object[] arguments = {

 new Integer(7),

 new Date(System.currentTimeMillis()),

 "a disturbance in the Force"

};

String result = MessageFormat.format(

 "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",

 arguments);

output: At 12:30 PM on Jul 3, 2053, there was a disturbance

       in the Force on planet 7.

Sadly, if you try to do something similar with Apex it always fails for StringException.

  • 1
    Gaurav -- please use the code formatting tools to reformat your answer – cropredy Sep 7 '16 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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