+ Start a Discussion
RamakarryRamakarry 

Datetime conversion from GMT to localuser time zone format

Hai friends !
I ve displayed  map of events on a visualforce page using following code snippet...But here the issue is.......
I wana dispaly Startdatetime, Enddatetime fileds on visualforce page in local user time zone. Can any one help how do modify the bellow code which is currently displaying the datetime in GMT formate only?
Code:
<apex:page standardController="Event" extensions="mysamplecls" sidebar="false"  setup="false" showHeader="false">  
<apex:pagemessages /> 
<style>
.ct{
text-align:center;
}
</style>
   <apex:form > 
     <apex:pageBlock >
       <apex:pageBlockSection >       
         <apex:inputfield value="{!Event.startDateTime}" /><br/>
         <apex:inputField value="{!Event.EndDateTime}"/>
        </apex:pageBlockSection> 
      <apex:pageBlockButtons location="bottom">
           <apex:commandButton value="Search" action="{!Query}"/>
      </apex:pageBlockButtons>
      </apex:pageBlock>
      <apex:pageBlock title="Event List" id="pb" rendered="{!abool}">
      <apex:pageblockSection columns="1" >      
     <apex:pageBlockTable value="{!eventmap}" var="a"  rendered="{!(Events.size != 0)}">  
        
  
      <apex:column headerValue="Subject" width="1%" > <br/><br/>  
       <apex:repeat value="{!eventmap[a]}" var="e"> 
        <table>    
        
        <tr><td >{!e.Subject}</td></tr>       
               </table>  
        </apex:repeat>
        </apex:column>
      
        
        <apex:column headerValue="startdatetime" width="1%"> <br/><br/>      
        <apex:repeat value="{!eventmap[a]}" var="e">
        <table Width="50%">
        <tr width="1px"><td >
         <apex:outputText value="{0,date,dd/MM/yyyy HH:mm a}">
    <apex:param value="{!e.startdatetime}" /> 
</apex:outputText>
        
        </td></tr>       
        </table>
        </apex:repeat>
        </apex:column>
        
        <apex:column headerValue="Enddatetime" width="1%"> <br/>
        <table Width="50%"><th>
        <apex:outputText value="{!a}" style="font-weight:800" /><br/>
        </th></table>
        <apex:repeat value="{!eventmap[a]}" var="e">
        <table Width="50%">
        <tr><td><apex:outputText value="{0,date,dd/MM/yyyy HH:mm a}">
    <apex:param value="{!e.Enddatetime}" /> 
</apex:outputText></td></tr>       
        </table>
        </apex:repeat>
        </apex:column>
        <apex:column headerValue="Name" width="1%">  <br/><br/>  
        <apex:repeat value="{!eventmap[a]}" var="e">
        <table Width="50%">
        <tr><td>{!e.owner.name}</td></tr>  
        </table>
        </apex:repeat>
        </apex:column>  
</apex:pageBlockTable>    
</apex:pageBlockSection>
</apex:pageblock>
<table><tr><td align="center">
 <apex:outputText rendered="{!(Events.size = 0)}"  value="There are no Events to display."  />
 </td>
 </tr>
 </table>
 </apex:form>
</apex:page>
controller:
public with sharing class mysamplecls{ 
public Event e;    
public list<Event> Events{set;get;}
public  map<string, List<Event>> eventmap{set;get;}
 public Boolean abool {get;set;}
 public mysamplecls(ApexPages.StandardController controller) {
    e=(Event)controller.getrecord(); 
    }
//call the fetched Events to be display 

//fetch the Events between two required dates
public PageReference Query() { 
abool = false;
if(e.StartDateTime<e.EndDateTime){
Events=new list<Event>();
Events.clear();  
 eventmap=new map<string, List<Event>>();
 system.debug('eventmap'+eventmap); 
Events = [Select Subject,StartDateTime,EndDateTime,Owner.Name,CreatedDate from Event where StartDateTime >=: e.StartDateTime and EndDateTime<=:e.EndDateTime order BY StartDateTime];
system.debug('list of Events'+Events );
if(Events.size()>0){abool = true;}
Datetime d;
for (Event e : Events){ 
            
    if(e.StartDateTime!=null )
          {
           d=e.startdatetime; 
           list<event> mapevents=new list<event>();     
            if(d==e.startdatetime)
                  { 
                   TimeZone tz=UserInfo.getTimeZone();
            
                   mapevents.add(e);                  
                }             
                             
             if(eventmap.containskey(string.ValueOf(d.format('dd-MM-yyyy-EEEE')))){
             eventmap.get(string.ValueOf(d.format('dd-MM-yyyy-EEEE'))).add(e); 
 
         System.debug('mapelementstype1'+eventmap);
       }
else
eventmap.put(string.ValueOf(d.format('dd-MM-yyyy-EEEE')),mapevents );
    
system.debug('Event Map type2'+eventmap);
               }
                             
            }
          }
       
       else
       {
        if((e.StartDateTime==null || e.EndDateTime==null))
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please Enter valid Date'));
        else
        if(e.StartDateTime>e.EndDateTime)
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'End date should be grater than start Date'));
        }
        return null;
       
    }
    }

 
Manj_SFDCManj_SFDC
Hi , Use the outputField instead of outputText it will render the date in the end user's timezone
VamsiVamsi
Thank you Manj_SFDC. It worked like champ !!!!
Manj_SFDCManj_SFDC
good to know Vamshi !, please mark as solved if it helped you , thanks
VamsiVamsi
Hi, I can't make this as best answer because I didn't raised this question.

Thank you.