Resolved WW-2326 - add timezone attribute to Date tag

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@923696 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Lukasz Lenart
2010-03-16 11:24:41 +00:00
parent a2f8645555
commit 9b495f00b7
4 changed files with 82 additions and 28 deletions
@@ -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;
/**
* <!-- START SNIPPET: javadoc -->
@@ -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;
}
}
@@ -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;
}
}
+8
View File
@@ -65,6 +65,14 @@ Please do not edit it directly.
<td align="left" valign="top">Boolean</td>
<td align="left" valign="top">Whether to print out the date nicely</td>
</tr>
<tr>
<td align="left" valign="top">timezone</td>
<td align="left" valign="top">false</td>
<td align="left" valign="top"></td>
<td align="left" valign="top">false</td>
<td align="left" valign="top">String</td>
<td align="left" valign="top">The specific timezone in which to format the date</td>
</tr>
<tr>
<td align="left" valign="top">var</td>
<td align="left" valign="top">false</td>
@@ -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();