Wednesday, April 4, 2012

Different Date Formats Using Apex and Visualforce

APEX:

public with sharing class clsDateFormat {

Datetime myDateTime = system.now();

  List<String> dateFormats = new List<String> {
     'dd.MM.yy HH:mm:ss',
     'MMMMM dd, yyyy hh:mm:ss a',
     'MMM-dd-yyyy hh:mm a',
     'EEEEE dd MMMMM yyyy G',
     'E hh:mm:ss:SSS z',
     'zzzzz (Z), \'Day of the year:\' D'
  };
   
  public DateTime getMyDateTime(){
    return myDateTime;
  }

  public List<String> getDateFormats(){
    return dateFormats;
  }

}

VisualForce:

<apex:page controller="clsDateFormat">
  <apex:sectionHeader title="Date Formatting"/>
    
    <apex:outputText value="Standard Output Format: {!myDateTime}"/>
    
    <apex:pageBlock >
        <apex:pageBlockTable value="{!dateFormats}" var="dateFormat">
            <apex:column headerValue="Date Format" value="{!dateFormat}" width="50%"/>
            <apex:column headerValue="Output" width="50%">
                <apex:outputText value="{0,date,{!dateFormat}}">
                    <apex:param value="{!myDateTime}" />
                </apex:outputText>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Demo https://sfdevforce-developer-edition.na12.force.com/DateFormat

No comments:

Post a Comment