+ Start a Discussion
acrozieracrozier 

Formatting outputText to show respective users Time Zone

Hello,

 

I found a previous post referencing this but cannot seem to get it to work in my org.  I am already using an Extension on this page to reference users information from names in a picklist.  I have tried to put two extensions, such as extensions="ProjectConfirmationEXT,timeZone" but get the following error,  "Error: Unknown constructor 'timeZone.timeZone(ApexPages.StandardController controller)'"

 

Can someone help me figure out what I am doing wrong?

 

VisualForce page

 

<apex:page standardController="Job__c" extensions="ProjectConfirmationEXT" showHeader="false" renderAs="pdf">

<apex:dataTable style="font-family: Calibri, sans-serif;" id="EventTable" value="{!Job__c.Events}" var="e" width="100%" rowClasses="odd,even" styleClass="tableClass" cellpadding="4" border="1">     
        
        <apex:column headerValue="Date">
            <apex:outputField value="{0,date,MM/dd/yyyy}">
                <apex:param value="{!e.StartDateTime}" />
            </apex:outputField>
        </apex:column>
        <apex:column headerValue="Start Time">
            <apex:outputText value="{0,time,HH:MM}">
                <apex:param value="{!e.StartDateTime}" />
            </apex:outputText>
        </apex:column>
        <apex:column headerValue="End Time">
            <apex:outputText value="{0,time,HH:MM}">
                <apex:param value="{!e.EndDateTime}" />
            </apex:outputText>
        </apex:column>
        
    </apex:dataTable>

 

timeZone EXT

 

public class timeZone {
        public String dateTimeValue { get; set; }
        public timeZone() {
            dateTimeValue = System.Now().format('MM/dd/yy HH:mm a', 'PST');//GMT
        }
}

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Greg HGreg H

 

Hi Andrew,
I received your email yesterday about this topic. I decided it would be better to post my response here so more developers could benefit from the discussion.
In this case I think you are heading down the wrong path by trying to format the field values for display from within your extension. This is a good example of where to use a component within your Visualforce page in order to format many values in the same manner in more than one location within the Visualforce page itself. By using a component you can pass the value you want to format to the component, process it (or manipulate it in this case) and then render it as needed.
First, you'll need to define the controller for the component:
public class controller_formatted_datetime {
	
	public DateTime date_time { get; set; } //property that reads the datetime value from the component attribute tag
	public String defined_format { get; set; } //property that reads the string value from the component attribute tag
	
	public String getFormattedDatetime() {
		if (date_time == null) { //if no date_time value is provided
			return ''; //return blank string
		} else { //otherwise, a date_time value was provided
			if (defined_format == null) { //if the format for the date_time value was not provided
				return date_time.format(); //return the full date/time for the user based upon the locale and time zone (the format() method provides the string in the running user's time zone and locale)
			} else { //otherwise, the format was provided
				return date_time.format(defined_format); //return the date_time value formatted as requested but with the locale and time zone taken into consideration
			}
		}
	}
}

 

In order to keep things simple you can create a new Apex class for the controller listed above, which should be separate from your class defined within your current Visualforce page. Then you'll need to define the component itself (Setup > Develop > Components > New):
<apex:component access="global" controller="controller_formatted_datetime"> 
	<apex:attribute assignTo="{!date_time}" description="The DateTime value to be rendered" name="date_time_value" type="DateTime"></apex:attribute>
	<apex:attribute assignTo="{!defined_format}" description="The optional format to use for the DateTime value to be rendered" name="date_time_format" type="String"></apex:attribute>
	{!FormattedDatetime}
</apex:component>

 

Last, you'll need to alter your Visualforce page in order to utilize the component. This can be accomplished by adding the following tag wherever you need to format a Datetime value:
<c:formatted_datetime date_time_value="{!Object.Value}" date_time_format="HH:MM"></c:formatted_datetime>

 

Of course you'll need to be sure that the name used in the component reference tag matches the name you use when you actually define the component. In this case I am defining the component as "formatted_datetime" but you could easily call it anything you prefer.
Also, you'll need to change the "{!Object.Value}" reference and "HH:MM" within the code for the exact field you want to be formatted and how it should look. So your final Visualforce page would look something like this:
<apex:page standardController="Job__c" extensions="ProjectConfirmationEXT" showHeader="false" renderAs="pdf">

<apex:dataTable style="font-family: Calibri, sans-serif;" id="EventTable" value="{!Job__c.Events}" var="e" width="100%" rowClasses="odd,even" styleClass="tableClass" cellpadding="4" border="1">
	<apex:column headerValue="Date">
		<c:formatted_datetime date_time_value="{!e.StartDateTime}" date_time_format="MM/dd/yyyy"></c:formatted_datetime>
	</apex:column>
	<apex:column headerValue="Start Time">
		<c:formatted_datetime date_time_value="{!e.StartDateTime}" date_time_format="HH:MM"></c:formatted_datetime>
	</apex:column>
	<apex:column headerValue="End Time">
		<c:formatted_datetime date_time_value="{!e.EndDateTime}" date_time_format="HH:MM"></c:formatted_datetime>
	</apex:column>
</apex:dataTable>
</apex:page>

 

As you can see, the code I've provided allows you some flexibility with the display of each Datetime field. If you decide that you want to allow the system to format the full date/time based on a User's locale or time zone then you can simply leave that date_time_format attribute off the component tag.
-greg

 

All Answers

acrozieracrozier

OK, so I got the EXT to load, but it does not appear to work, thoughts?

 

New Extension code

 

 

public with sharing class timeZone {

    public timeZone(ApexPages.StandardController controller) {

    }

        public String dateTimeValue { get; set; }
        public timeZone() {
            dateTimeValue = System.Now().format('MM/dd/yy HH:mm a', 'PST');//GMT
        }
}

 

 

acrozieracrozier

So now I understand this will only give me the current time.  How can I format the Start dateTime field off of the Events object to display with time zone awareness?

Greg HGreg H

 

Hi Andrew,
I received your email yesterday about this topic. I decided it would be better to post my response here so more developers could benefit from the discussion.
In this case I think you are heading down the wrong path by trying to format the field values for display from within your extension. This is a good example of where to use a component within your Visualforce page in order to format many values in the same manner in more than one location within the Visualforce page itself. By using a component you can pass the value you want to format to the component, process it (or manipulate it in this case) and then render it as needed.
First, you'll need to define the controller for the component:
public class controller_formatted_datetime {
	
	public DateTime date_time { get; set; } //property that reads the datetime value from the component attribute tag
	public String defined_format { get; set; } //property that reads the string value from the component attribute tag
	
	public String getFormattedDatetime() {
		if (date_time == null) { //if no date_time value is provided
			return ''; //return blank string
		} else { //otherwise, a date_time value was provided
			if (defined_format == null) { //if the format for the date_time value was not provided
				return date_time.format(); //return the full date/time for the user based upon the locale and time zone (the format() method provides the string in the running user's time zone and locale)
			} else { //otherwise, the format was provided
				return date_time.format(defined_format); //return the date_time value formatted as requested but with the locale and time zone taken into consideration
			}
		}
	}
}

 

In order to keep things simple you can create a new Apex class for the controller listed above, which should be separate from your class defined within your current Visualforce page. Then you'll need to define the component itself (Setup > Develop > Components > New):
<apex:component access="global" controller="controller_formatted_datetime"> 
	<apex:attribute assignTo="{!date_time}" description="The DateTime value to be rendered" name="date_time_value" type="DateTime"></apex:attribute>
	<apex:attribute assignTo="{!defined_format}" description="The optional format to use for the DateTime value to be rendered" name="date_time_format" type="String"></apex:attribute>
	{!FormattedDatetime}
</apex:component>

 

Last, you'll need to alter your Visualforce page in order to utilize the component. This can be accomplished by adding the following tag wherever you need to format a Datetime value:
<c:formatted_datetime date_time_value="{!Object.Value}" date_time_format="HH:MM"></c:formatted_datetime>

 

Of course you'll need to be sure that the name used in the component reference tag matches the name you use when you actually define the component. In this case I am defining the component as "formatted_datetime" but you could easily call it anything you prefer.
Also, you'll need to change the "{!Object.Value}" reference and "HH:MM" within the code for the exact field you want to be formatted and how it should look. So your final Visualforce page would look something like this:
<apex:page standardController="Job__c" extensions="ProjectConfirmationEXT" showHeader="false" renderAs="pdf">

<apex:dataTable style="font-family: Calibri, sans-serif;" id="EventTable" value="{!Job__c.Events}" var="e" width="100%" rowClasses="odd,even" styleClass="tableClass" cellpadding="4" border="1">
	<apex:column headerValue="Date">
		<c:formatted_datetime date_time_value="{!e.StartDateTime}" date_time_format="MM/dd/yyyy"></c:formatted_datetime>
	</apex:column>
	<apex:column headerValue="Start Time">
		<c:formatted_datetime date_time_value="{!e.StartDateTime}" date_time_format="HH:MM"></c:formatted_datetime>
	</apex:column>
	<apex:column headerValue="End Time">
		<c:formatted_datetime date_time_value="{!e.EndDateTime}" date_time_format="HH:MM"></c:formatted_datetime>
	</apex:column>
</apex:dataTable>
</apex:page>

 

As you can see, the code I've provided allows you some flexibility with the display of each Datetime field. If you decide that you want to allow the system to format the full date/time based on a User's locale or time zone then you can simply leave that date_time_format attribute off the component tag.
-greg

 

This was selected as the best answer
MIGUEL PARDOMIGUEL PARDO
Thank you, very helpful, i have implemented in my sandbox , but i'd ask if there is a simple form to do this.This post is almost five years older
Charles ThompsonCharles Thompson
Excellent component.  I have used it at my customer to replace the default Related List for Activities with a custom VF component that looks and behaves similarly but has more flexibility.   Many thanks, @Greg H