diff --git a/core/src/main/java/org/apache/struts2/components/Date.java b/core/src/main/java/org/apache/struts2/components/Date.java index 672f36c54..9d82563fa 100644 --- a/core/src/main/java/org/apache/struts2/components/Date.java +++ b/core/src/main/java/org/apache/struts2/components/Date.java @@ -302,11 +302,7 @@ public class Date extends ContextBean { if (nice) { msg = formatTime(tp, date); } else { - TimeZone tz = TimeZone.getDefault(); - if (timezone != null) { - tz = TimeZone.getTimeZone(timezone); - } - + TimeZone tz = getTimeZone(); if (format == null) { String globalFormat = null; @@ -353,6 +349,19 @@ public class Date extends ContextBean { return super.end(writer, ""); } + private TimeZone getTimeZone() { + TimeZone tz = TimeZone.getDefault(); + if (timezone != null) { + timezone = stripExpressionIfAltSyntax(timezone); + String actualTimezone = (String) getStack().findValue(timezone, String.class); + if (actualTimezone != null) { + timezone = actualTimezone; + } + tz = TimeZone.getTimeZone(timezone); + } + return tz; + } + @StrutsTagAttribute(description="Date or DateTime format pattern", rtexprvalue=false) public void setFormat(String format) { this.format = format; diff --git a/core/src/test/java/org/apache/struts2/views/jsp/ui/DateTagTest.java b/core/src/test/java/org/apache/struts2/views/jsp/ui/DateTagTest.java index 108ed61a5..06de0febd 100644 --- a/core/src/test/java/org/apache/struts2/views/jsp/ui/DateTagTest.java +++ b/core/src/test/java/org/apache/struts2/views/jsp/ui/DateTagTest.java @@ -70,6 +70,24 @@ public class DateTagTest extends AbstractTagTest { assertEquals(formatted, writer.toString()); } + public void testCustomFormatWithTimezoneAsExpression() throws Exception { + String format = "yyyy/MM/dd hh:mm:ss"; + Date now = Calendar.getInstance(TimeZone.getTimeZone("UTC+2")).getTime(); + SimpleDateFormat sdf = new SimpleDateFormat(format); + sdf.setTimeZone(TimeZone.getTimeZone("UTC+2")); + String formatted = sdf.format(now); + context.put("myDate", now); + context.put("myTimezone", "UTC+2"); + + tag.setName("myDate"); + tag.setNice(false); + tag.setFormat(format); + tag.setTimezone("myTimezone"); + tag.doStartTag(); + tag.doEndTag(); + assertEquals(formatted, writer.toString()); + } + public void testCustomFormatCalendar() throws Exception { String format = "yyyy/MM/dd hh:mm:ss"; Calendar calendar = Calendar.getInstance();