You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by bu...@apache.org on 2003/12/23 09:01:26 UTC

DO NOT REPLY [Bug 25719] New: - timezone support for bean:write tag

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=25719>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=25719

timezone support for bean:write tag 

           Summary: timezone support for bean:write tag
           Product: Struts
           Version: 1.1RC2
          Platform: All
        OS/Version: Other
            Status: NEW
          Severity: Enhancement
          Priority: Other
         Component: Custom Tags
        AssignedTo: struts-dev@jakarta.apache.org
        ReportedBy: damncho@yahoo.com


currently as of version 1.2 of Struts custom tag library, timezone is not
configurable for using bean:write tag with Date object. Both JSTL and apache
date custom tags support converting date value to that of different timezone. 

below is a subclass I created to accomodate timezone configuration. Something
like this would do..

public class WriteDateTag extends WriteTag {

	private String timeZoneString;
	
	public void setTimezone(String timeZoneString) {
		this.timeZoneString = timeZoneString;
	}
	
	public String getTimezoneString() {
		return timeZoneString;
	}
	
	/**
	 * Format value according to specified format string (as tag attribute or
	 * as string from message resources) or to current user locale.
	 * 
	 * @param valueToFormat value to process and convert to String
	 * @exception JspException if a JSP exception has occurred
	 */
	protected String formatValue(Object valueToFormat) throws JspException {
		Format format = null;
		Object value = valueToFormat;
		Locale locale =
			RequestUtils.retrieveUserLocale(pageContext, this.localeKey);
		boolean formatStrFromResources = false;
		String formatString = formatStr;

		// Try to retrieve format string from resources by the key from formatKey.
		if( ( formatString==null ) && ( formatKey!=null ) ) {
			formatString = retrieveFormatString( this.formatKey );
			if( formatString!=null )
				formatStrFromResources = true;
		}

		if (formatString == null) {

			if (value instanceof java.sql.Timestamp) {
				formatString = retrieveFormatString(SQL_TIMESTAMP_FORMAT_KEY);
			} else if (value instanceof java.sql.Date) {
				formatString = retrieveFormatString(SQL_DATE_FORMAT_KEY);
			} else if (value instanceof java.sql.Time) {
				formatString = retrieveFormatString(SQL_TIME_FORMAT_KEY);
			} else if (value instanceof java.util.Date) {
				formatString = retrieveFormatString(DATE_FORMAT_KEY);
			}

			if (formatString != null)
				formatStrFromResources = true;

		}
		
		TimeZone tz = null;
		if (timeZoneString != null) {
			tz = TimeZone.getTimeZone(timeZoneString);
		} else {
			tz = TimeZone.getDefault();
		}

		if (formatString != null) {
			if (formatStrFromResources) {
				format = new SimpleDateFormat(formatString, locale);
			} else {
				format = new SimpleDateFormat(formatString);
			}
		}
		
		
		if (format != null) {
			((SimpleDateFormat) format).setTimeZone(tz);
			return format.format(value);
		} else {
			return value.toString();
		}
	}
}

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-dev-help@jakarta.apache.org