function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

Please Help with TimeZone Conversion

Hi Guys,

I have attempted to make a tool which converts a datetime based on timezone. I had an diea, but I have reached a few snags, I was hoping I could get a hand. 

Problem 1. I have tried Adding this to my event extension class (code 1), except whenever I load the Page it says invalid runtime error between Event and user. I am not sure if I need to access User in order to create the timezone picklist. Is there a way to get this picklist on an event page, without having to access the User object?

Problem 2: I figure the best method is to find your own timezone offset, remove your offset from the time and then add the offset of the timezone you select from the timezone picklist. Sounds simple enough in theory, but actually implementing it is hard. Please any advice or help in solving this problem would eb appreciated.

Goal:  Have picklist on event page, picklist will render when a checkbox (Different_Timezone__c) is checked, this will also be the criteria which will fill another two datetime fields which are the converted times of the start and end date time.

CODE 1
 
Public class extSaveEveButton {


public Event Eve {get;set;}

Datetime mydate = System.now();

String S = mydate.format('dd/MM/yyyy');
String T = mydate.format(' hh:mm aa');
String H = T.substring(1, 3);
String M = T.substring(4, 6);
String AMPM = T.substring(7, 9);


//Introduce strings and set format
//Start
String st = T;
String sh = H;
String sm = M;
String sAMPM = AMPM;
String sd = S;
//End
String et = T;
String eh = H;
String em = M;
String eAMPM = AMPM;
String ed = S;
//Reminder
String rt = T;
String rh = H;
String rm = M;
String rAMPM = AMPM;
String rd = S;
//Follow-Up
String ft = T;
String fh = H;
String fm = M;
String fAMPM = AMPM;
String fd = S;

//Sets Standard Controller
public extSaveEveButton (ApexPages.StandardController controller) {
this.Eve = (Event)controller.getRecord();


}

//Retrieve instance of strings within VF page
//Start
//Time
//Hours
public String getStartHours(){return sh;}
public void setStartHours(String sh) {this.sh = sh;}
//Mins
public String getStartMins(){return sm;}
public void setStartMins(String sm) {this.sm = sm;}
//AMPM
public String getStartAMPM(){return sAMPM;}
public void setStartAMPM(String sAMPM) {this.sAMPM = sAMPM;}
//Time
public String getStartTime(){return st;}
public void setStartTime(String st) {this.st = st;}
//Date
public String getStartDate() {return sd;}
public void setStartDate(String sd) {this.sd = sd;}

//End
//Hours
public String getEndHours(){return eh;}
public void setEndHours(String eh) {this.eh = eh;}
//Mins
public String getEndMins(){return em;}
public void setEndMins(String em) {this.em = em;}
//AMPM
public String getEndAMPM(){return eAMPM;}
public void setEndAMPM(String eAMPM) {this.eAMPM = eAMPM;}
//Time
public String getEndTime(){return et;}
public void setEndTime(String et) {this.et = et;}
//Date
public String getEndDate() {return ed;}
public void setEndDate(String ed) {this.ed = ed;}

//Reminder
//Hours
public String getReminderHours(){return rh;}
public void setReminderHours(String rh) {this.rh = rh;}
//Mins
public String getReminderMins(){return rm;}
public void setReminderMins(String rm) {this.rm = rm;}
//AMPM
public String getReminderAMPM(){return rAMPM;}
public void setReminderAMPM(String rAMPM) {this.rAMPM = rAMPM;}
//Time
public String getReminderTime(){return rt;}
public void setReminderTime(String rt) {this.rt = rt;}
//Date
public String getReminderDate() {return rd;}
public void setReminderDate(String rd) {this.rd = rd;}

//Follow Up
//Hours
public String getFollowHours(){return fh;}
public void setFollowHours(String fh) {this.fh = fh;}
//Mins
public String getFollowMins(){return fm;}
public void setFollowMins(String fm) {this.fm = fm;}
//AMPM
public String getFollowAMPM(){return fAMPM;}
public void setFollowAMPM(String fAMPM) {this.fAMPM = fAMPM;}
//Time
public String getFollowTime(){return ft;}
public void setFollowTime(String ft) {this.ft = ft;}
//Date
public String getFollowDate() {return fd;}
public void setFollowDate(String fd) {this.fd = fd;}

//Save Button
Public PageReference saveDestinyEvent(){

//Clone Situation
if(System.currentPageReference().getParameters().get('clone') == '1'){
//Set record id to null
Eve.Id = null;
}


//Set Start and End Datetime
if(Eve.StartDateTime == null){
String S = sd+' '+this.sh+':'+this.sm+' '+this.sAMPM;
Eve.StartDateTime = DateTime.parse(S);
}
if(Eve.EndDateTime == null){
String EdEt = ed+' '+this.eh+':'+this.em+' '+this.eAMPM;
Eve.EndDateTime = DateTime.parse(EdEt);
}
if((Eve.IsReminderSet == true)&&(Eve.ReminderDateTime == null)){
String R = this.rd+' '+this.rh+':'+this.rm+' '+this.rAMPM;
Eve.ReminderDateTime= DateTime.parse(R);
}
if(Eve.Destiny_Service_Decision__c=='Follow Up'){
String F = this.fd+' '+this.fh+':'+this.fm+' '+this.fAMPM;
String FoD = this.fd;
Eve.Follow_Up_DateTime__c = DateTime.parse(F);
Eve.Follow_Up_Date__c = Date.parse(FoD);
Eve.Follow_Up_Time__c = this.ft;
}

try {     
Upsert Eve;
// Send the user to the detail page for the new account.
PageReference pageRef= new PageReference('/apex/DestinyAccount?id='+Eve.whatid+'&Sfdc.override=1');
pageRef.getParameters().put('tab','Activities');
return pageRef;
 } catch (Exception e) {     
      
             ApexPages.addMessages(e);      
        }
        return null;

}

//Save and New Button
Public PageReference saveNewDestinyEvent(){

//Set Start and End Datetime
if(Eve.StartDateTime == null){
String S = sd+' '+this.sh+':'+this.sm+' '+this.sAMPM;
Eve.StartDateTime = DateTime.parse(S);
}
if(Eve.EndDateTime == null){
String EdEt = ed+' '+this.eh+':'+this.em+' '+this.eAMPM;
Eve.EndDateTime = DateTime.parse(EdEt);
}
if((Eve.IsReminderSet == true)&&(Eve.ReminderDateTime == null)){
String R = this.rd+' '+this.rh+':'+this.rm+' '+this.rAMPM;
Eve.ReminderDateTime= DateTime.parse(R);
}
if(Eve.Destiny_Service_Decision__c=='Follow Up'){
String F = this.fd+' '+this.fh+':'+this.fm+' '+this.fAMPM;
String FoD = this.fd;
Eve.Follow_Up_DateTime__c = DateTime.parse(F);
Eve.Follow_Up_Date__c = Date.parse(FoD);
Eve.Follow_Up_Time__c = this.ft;
}


try { 
Upsert Eve;
// Send the user to the detail page for the new account.
PageReference pageRef= new PageReference('/00T/e');
return pageRef;
 } catch (Exception e) {     
      
             ApexPages.addMessages(e);         
        }
        return null;

}

//Cancel Button
Public PageReference cancelDestinyEvent(){
PageReference pageRef = new PageReference('/apex/DestinyAccount?id='+Eve.whatid+'&Sfdc.override=1');
pageRef.getParameters().put('tab','Activities');
return pageRef;

}

   
///////////////////////////////////////////////////Time tool\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// Time SelectList
public list<selectoption> getTimes()
{
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption(T,T));
      options.add(new SelectOption('12:00 PM','12:00 PM'));
      options.add(new SelectOption('1:00 PM','1:00 PM'));
      options.add(new SelectOption('2:00 PM','2:00 PM'));
      options.add(new SelectOption('3:00 PM','3:00 PM'));
      options.add(new SelectOption('4:00 PM','4:00 PM'));
      options.add(new SelectOption('5:00 PM','5:00 PM'));
      options.add(new SelectOption('6:00 PM','6:00 PM'));
      options.add(new SelectOption('7:00 PM','7:00 PM'));
      options.add(new SelectOption('8:00 PM','8:00 PM'));
      options.add(new SelectOption('9:00 PM','9:00 PM'));
      options.add(new SelectOption('10:00 PM','10:00 PM'));
      options.add(new SelectOption('11:00 PM','11:00 PM'));
      options.add(new SelectOption('12:00 AM','12:00 AM'));
      options.add(new SelectOption('1:00 AM','1:00 AM'));
      options.add(new SelectOption('2:00 AM','2:00 AM'));
      options.add(new SelectOption('3:00 AM','3:00 AM'));
      options.add(new SelectOption('4:00 AM','4:00 AM'));
      options.add(new SelectOption('5:00 AM','5:00 AM'));
      options.add(new SelectOption('6:00 AM','6:00 AM'));
      options.add(new SelectOption('7:00 AM','7:00 AM'));
      options.add(new SelectOption('8:00 AM','8:00 AM'));
      options.add(new SelectOption('9:00 AM','9:00 AM'));
      options.add(new SelectOption('10:00 AM','10:00 AM'));
      options.add(new SelectOption('11:00 AM','11:00 AM'));
      options.add(new SelectOption('12:00 AM','12:00 AM'));

   return options;
 }
 
 public list<selectoption> getHours()
{
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption(H,H));
      options.add(new SelectOption('12','12'));
      options.add(new SelectOption('1','1'));
      options.add(new SelectOption('2','2'));
      options.add(new SelectOption('3','3'));
      options.add(new SelectOption('4','4'));
      options.add(new SelectOption('5','5'));
      options.add(new SelectOption('6','6'));
      options.add(new SelectOption('7','7'));
      options.add(new SelectOption('8','8'));
      options.add(new SelectOption('9','9'));
      options.add(new SelectOption('10','10'));
      options.add(new SelectOption('11','11'));
   return options;
 }  
 
 public list<selectoption> getMins()
{
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('00','00'));
      options.add(new SelectOption('00','00'));
      options.add(new SelectOption('05','05'));
      options.add(new SelectOption('10','10'));
      options.add(new SelectOption('15','15'));
      options.add(new SelectOption('20','20'));
      options.add(new SelectOption('25','25'));
      options.add(new SelectOption('30','30'));
      options.add(new SelectOption('30','30'));
      options.add(new SelectOption('35','35'));
      options.add(new SelectOption('40','40'));
      options.add(new SelectOption('45','45'));
      options.add(new SelectOption('50','50'));
      options.add(new SelectOption('55','55'));
   
   return options;
 }    
 
  public list<selectoption> getAMPM()
{
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('PM','PM'));
      options.add(new SelectOption('AM','AM')); 
   return options;
 } 
///////////////////////////////////////////////////Time tool\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 

}



CODE 2
<apex:page standardController="User" extensions="Sample2">

    <apex:sectionHeader title="Visualforce Sample" subtitle="Describe for Picklist Values" help="/help/doc/user_ed.jsp?loc=help"></apex:sectionHeader>
    <apex:form >
    
    
        <apex:pageBlock title="Criteria" mode="edit">
            <apex:pageBlockSection title="Information" columns="1">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Time Zones" for="time_zones"></apex:outputLabel>
                    <apex:actionRegion > 
                    <apex:selectList id="time_zones" size="1" value="{!TimeZoneUser}" title="Time Zones">
                        <apex:selectOptions value="{!TimeZones}"></apex:selectOptions>
                        <apex:actionSupport event="onchange" reRender="ajaxrequest1" />
                    </apex:selectList>
                        </apex:actionRegion>
</apex:pageBlockSectionItem>


            </apex:pageBlockSection>
            <apex:outputpanel id="ajaxrequest1" > 
            
            <!---->  

              <apex:pageBlockSection columns="1">
      {!LocalOffset}



            </apex:pageBlockSection>
                    <apex:pageBlockSection columns="1">

     {!TimeZoneUser}
 

            </apex:pageBlockSection>
                    <apex:pageBlockSection columns="1">

     {!OffsetString}


            </apex:pageBlockSection>
                                <apex:pageBlockSection columns="1">

     {!CurrentTime}
     {!CurrentTime2}


            </apex:pageBlockSection>
                                            <apex:pageBlockSection columns="1">

     {!TimeZoneTime}


            </apex:pageBlockSection>
                                <apex:pageBlockSection columns="1">

     {!Offset}


            </apex:pageBlockSection>
            
          </apex:outputpanel> 
        
        </apex:pageBlock>
    </apex:form>
</apex:page>
Code 3
 
public with sharing class Sample2{
    private String first_picklist_option = '- All -'; //first value to display in picklist
    private final User user_object; //User sobject
    TimeZone UserTz = UserInfo.getTimeZone();
    TimeZone ClientTz = UserTz;
    String TimeZoneString = string.valueof(UserTz);
    public Integer iTz2;
    public Integer iTz3;
 
   
    Datetime mydate = System.now();
        integer test = UserTz.getOffset(mydate);

    
    public integer LocalOffset {get;set;}
    
    String eh = TimeZoneString;
    
    
    public String getTimeZoneUser(){return eh;}
public void setTimeZoneUser(String eh) {this.eh = eh;}

public string getOffsetString(){ 
return eh.substring(4,10);

}

public integer getOffset(){
if(eh == 'Pacific/Chatham'){
return 0;
}else{ 
String Tz1 = eh.substring(4,5);
String Tz2 = eh.substring(5,6);
String Tz3 = eh.substring(6,7);
String Tz4 = eh.substring(8,9);
String Tz5 = eh.substring(9,10);
return integer.valueof(Tz2+Tz3+Tz4+Tz5);
}



}

public datetime getCurrentTime(){ 
return mydate;

}

public Date getCurrentTime2(){ 
DateTime myDateTime = DateTime.newInstance(2006, 3, 16, 23, 0, 0);
Date myDate2 = myDateTime.dateGMT();
return myDate2;

}

public datetime getTimeZoneTime(){
if(eh == 'Australia/Brisbane'){
return mydate;
}else{ 
String Tz1 = eh.substring(4,5);
String Tz2 = eh.substring(5,6);
String Tz3 = eh.substring(6,7);
String Tz4 = eh.substring(8,9);
String Tz5 = eh.substring(9,10);
//Integer Hours = integer.valueof(Tz2+Tz3);
//Integer Minutes = integer.valueof(Tz4+Tz5);

if(Tz1 == '+'){
return mydate.addHours(integer.valueof(Tz2+Tz3)).addminutes(integer.valueof(Tz4+Tz5));
}else if(Tz1 == '-'){
return mydate.addHours(-(integer.valueof(Tz2+Tz3))).addminutes(-(integer.valueof(Tz4+Tz5)));
}else{
return null;
}

}
}

    
    public Sample2(ApexPages.StandardController stdController) {
        this.user_object = (User)stdController.getRecord();
        LocalOffset = Test;
         String TimeZoneString = '0';
         integer Offset = 0;

    }
    
    //builds a picklist of values based upon the passed information
    public List<selectOption> getPickValues(Sobject object_name, String field_name, String first_val) {
        List<selectOption> options = new List<selectOption>(); //new list for holding all of the picklist options
        if ( first_val != null ) { //if there is a first value being provided
            options.add(new selectOption(first_val,first_val)); //add the first option
        }
        Schema.sObjectType sobject_type = object_name.getSObjectType(); //grab the sobject that was passed
        Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe(); //describe the sobject
        Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap(); //get a map of fields for the passed sobject
        List<Schema.PicklistEntry> pick_list_values = field_map.get(field_name).getDescribe().getPickListValues(); //grab the list of picklist values for the passed field on the sobject
        for (Schema.PicklistEntry a : pick_list_values) { //for all values in the picklist list
            options.add(new selectOption(a.getLabel(), a.getValue()));  //add the value and label to our final list
        }
        return options; //return the List
    }
    
    
    //return the picklist options for User.TimeZoneSidKey
    public List<selectOption> getTimeZones() {
        return getPickValues(user_object, 'TimeZoneSidKey', first_picklist_option);
    }


}