[SalesForce] Timezone on VF Page

I have to Create one visualforce page,Query any one record and display Created Date on page in this format mm-dd-yyyy hh : mm using Use Format method, then format the dateTime to required format
Format method is avilable in class DAteTime
Can anyone help me out in this

VF Page :

<apex:page standardcontroller="account" extensions="timeZone">

<apex:dataTable style="font-family: Calibri, sans-serif;" value="{!details}" var="a" width="100%" styleClass="tableClass" cellpadding="4" border="1">     

        <apex:column headerValue="Date">
            <apex:outputText value="{0,date,MM/dd/yyyy,HH:mm}">
                <apex:param value="{!a.CreatedDate}" />
            </apex:outputText>
        </apex:column>

    </apex:dataTable>
</apex:page>

Controller :

public class timeZone {
public string cd;
        public timeZone(ApexPages.StandardController controller) {}
        public list<account> getdetails(){
    list<account> a = [SELECT id,CreatedDate FROM account where name = 'mal'];
   // String strConvertedDate = a.CreatedDate.format('MM/dd/yyyy HH:mm:ss', UserInfo.getTimeZone());
    return a;
}   

}

Best Answer

Reason: Salesforce store the dateTime value in database in GMT Format. When the dateTime display on standard page. It display in current user's time zone

Use in controller.. use strConvertedDate instead a.CreatedDate

DateTime format for current user

CreatedDate.format('MM/dd/yyyy,HH:mm', UserInfo.getTimeZone().getID()) or

CreatedDate.format('MM/dd/yyyy,HH:mm', UserInfo.getTimeZone().toString())

String strConvertedDate = a.CreatedDate.format('MM/dd/yyyy HH:mm:ss', UserInfo.getTimeZone().toString());

Edit

Controller

public class timeZone {
        public timeZone(ApexPages.StandardController controller) {}

        public List<wrapperAccount> getdetails(){

        List<wrapperAccount> lstwrapper = new List<wrapperAccount>();
    for(Account obj: [SELECT id, Name, CreatedDate FROM account]){
        lstwrapper.add(new wrapperAccount(obj));
    }

    return lstwrapper;
}   
    public class wrapperAccount{
        public Account objAccount {get; private set;}
        public String strFormattedDate {get; private set;}
        public wrapperAccount(Account objAccount){
        this.objAccount = objAccount;
        strFormattedDate = objAccount.CreatedDate.format('MM/dd/yyyy,HH:mm', UserInfo.getTimeZone().toString());
        }
    }
}

Page

<apex:page standardcontroller="account" extensions="timeZone">

<apex:dataTable style="font-family: Calibri, sans-serif;" value="{!details}" var="wrap" width="100%" styleClass="tableClass" cellpadding="4" border="1">     

        <apex:column headerValue="Date">
            <apex:outputText value="{!wrap.strFormattedDate}"/>
        </apex:column>

        <apex:column headerValue="Name">
            <apex:outputText value="{!wrap.objAccount.Name}"/>
        </apex:column>

    </apex:dataTable>
</apex:page>
Related Topic