What is the value of 0 in Apex outputtext value?

519    Asked by BuffyHeaton in Salesforce , Asked on May 11, 2023
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?

Answered by David EDWARDS

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.format in Java and apex you can say.....
under documented Apex String API i.e.
String formattedString = String.format(String template, List 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 Visualforce 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.

  
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.

Your Answer

Interviews

Parent Categories