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 5c198a197..672f36c54 100644 --- a/core/src/main/java/org/apache/struts2/components/Date.java +++ b/core/src/main/java/org/apache/struts2/components/Date.java @@ -21,6 +21,14 @@ package org.apache.struts2.components; +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.TextProvider; +import com.opensymphony.xwork2.util.ValueStack; +import com.opensymphony.xwork2.util.logging.Logger; +import com.opensymphony.xwork2.util.logging.LoggerFactory; +import org.apache.struts2.views.annotations.StrutsTag; +import org.apache.struts2.views.annotations.StrutsTagAttribute; + import java.io.IOException; import java.io.Writer; import java.text.DateFormat; @@ -29,15 +37,7 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.Iterator; import java.util.List; - -import org.apache.struts2.views.annotations.StrutsTag; -import org.apache.struts2.views.annotations.StrutsTagAttribute; - -import com.opensymphony.xwork2.ActionContext; -import com.opensymphony.xwork2.TextProvider; -import com.opensymphony.xwork2.util.ValueStack; -import com.opensymphony.xwork2.util.logging.Logger; -import com.opensymphony.xwork2.util.logging.LoggerFactory; +import java.util.TimeZone; /** * @@ -193,6 +193,8 @@ public class Date extends ContextBean { private boolean nice; + private String timezone; + public Date(ValueStack stack) { super(stack); } @@ -300,6 +302,11 @@ public class Date extends ContextBean { if (nice) { msg = formatTime(tp, date); } else { + TimeZone tz = TimeZone.getDefault(); + if (timezone != null) { + tz = TimeZone.getTimeZone(timezone); + } + if (format == null) { String globalFormat = null; @@ -312,18 +319,22 @@ public class Date extends ContextBean { // DATETAG_PROPERTY if (globalFormat != null && !DATETAG_PROPERTY.equals(globalFormat)) { - msg = new SimpleDateFormat(globalFormat, - ActionContext.getContext().getLocale()) - .format(date); + SimpleDateFormat sdf = new SimpleDateFormat(globalFormat, + ActionContext.getContext().getLocale()); + sdf.setTimeZone(tz); + msg = sdf.format(date); } else { - msg = DateFormat.getDateTimeInstance( + DateFormat df = DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.MEDIUM, - ActionContext.getContext().getLocale()) - .format(date); + ActionContext.getContext().getLocale()); + df.setTimeZone(tz); + msg = df.format(date); } } else { - msg = new SimpleDateFormat(format, ActionContext - .getContext().getLocale()).format(date); + SimpleDateFormat sdf = new SimpleDateFormat(format, ActionContext + .getContext().getLocale()); + sdf.setTimeZone(tz); + msg = sdf.format(date); } } if (msg != null) { @@ -377,4 +388,17 @@ public class Date extends ContextBean { public boolean isNice() { return nice; } + + /** + * @return Returns the name. + */ + public String getTimezone() { + return timezone; + } + + @StrutsTagAttribute(description = "The specific timezone in which to format the date", required = false) + public void setTimezone(String timezone) { + this.timezone = timezone; + } + } diff --git a/core/src/main/java/org/apache/struts2/views/jsp/DateTag.java b/core/src/main/java/org/apache/struts2/views/jsp/DateTag.java index c39baf5dc..9f2ea3f88 100644 --- a/core/src/main/java/org/apache/struts2/views/jsp/DateTag.java +++ b/core/src/main/java/org/apache/struts2/views/jsp/DateTag.java @@ -21,13 +21,12 @@ package org.apache.struts2.views.jsp; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - +import com.opensymphony.xwork2.util.ValueStack; import org.apache.struts2.components.Component; import org.apache.struts2.components.Date; -import com.opensymphony.xwork2.util.ValueStack; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; /** * @see Date @@ -39,6 +38,7 @@ public class DateTag extends ContextBeanTag { protected String name; protected String format; protected boolean nice; + protected String timezone; public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res) { return new Date(stack); @@ -50,7 +50,7 @@ public class DateTag extends ContextBeanTag { d.setName(name); d.setFormat(format); d.setNice(nice); - + d.setTimezone(timezone); } public void setFormat(String format) { @@ -64,4 +64,9 @@ public class DateTag extends ContextBeanTag { public void setName(String name) { this.name = name; } + + public void setTimezone(String timezone) { + this.timezone = timezone; + } + } diff --git a/core/src/site/resources/tags/date.html b/core/src/site/resources/tags/date.html index 11f4a448c..579fa187a 100644 --- a/core/src/site/resources/tags/date.html +++ b/core/src/site/resources/tags/date.html @@ -65,6 +65,14 @@ Please do not edit it directly. Boolean Whether to print out the date nicely + + timezone + false + + false + String + The specific timezone in which to format the date + var false 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 15d44aa18..108ed61a5 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 @@ -21,15 +21,15 @@ package org.apache.struts2.views.jsp.ui; +import com.opensymphony.xwork2.ActionContext; +import org.apache.struts2.views.jsp.AbstractTagTest; +import org.apache.struts2.views.jsp.DateTag; + import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; - -import org.apache.struts2.views.jsp.AbstractTagTest; -import org.apache.struts2.views.jsp.DateTag; - -import com.opensymphony.xwork2.ActionContext; +import java.util.TimeZone; /** * Unit test for {@link org.apache.struts2.components.Date}. @@ -52,7 +52,24 @@ public class DateTagTest extends AbstractTagTest { tag.doEndTag(); assertEquals(formatted, writer.toString()); } - + + public void testCustomFormatWithTimezone() throws Exception { + String format = "yyyy/MM/dd hh:mm:ss"; + Date now = Calendar.getInstance(TimeZone.getTimeZone("UTC+1")).getTime(); + SimpleDateFormat sdf = new SimpleDateFormat(format); + sdf.setTimeZone(TimeZone.getTimeZone("UTC+1")); + String formatted = sdf.format(now); + context.put("myDate", now); + + tag.setName("myDate"); + tag.setNice(false); + tag.setFormat(format); + tag.setTimezone("UTC+1"); + 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();