You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@struts.apache.org by "Matt Meola (JIRA)" <ji...@apache.org> on 2008/01/19 19:44:04 UTC

[jira] Commented: (WW-2326) Add timezone support to date component

    [ https://issues.apache.org/struts/browse/WW-2326?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=43043#action_43043 ] 

Matt Meola commented on WW-2326:
--------------------------------

How about this?  It works for 2.0.11...

- svn diff
Index: core/src/main/java/org/apache/struts2/components/Date.java
===================================================================
--- core/src/main/java/org/apache/struts2/components/Date.java  (revision 613415)
+++ core/src/main/java/org/apache/struts2/components/Date.java  (working copy)
@@ -28,6 +28,7 @@
 import java.util.Calendar;
 import java.util.Iterator;
 import java.util.List;
+import java.util.TimeZone;
 
 import org.apache.struts2.views.annotations.StrutsTag;
 import org.apache.struts2.views.annotations.StrutsTagAttribute;
@@ -192,6 +193,8 @@
 
     private boolean nice;
 
+    private String timezone;
+
     public Date(ValueStack stack) {
         super(stack);
     }
@@ -300,6 +303,10 @@
                 if (nice) {
                     msg = formatTime(tp, date);
                 } else {
+                       TimeZone tz = TimeZone.getTimeZone("UTC");
+                       if (timezone != null) {
+                               tz = TimeZone.getTimeZone(timezone);
+                       }
                     if (format == null) {
                         String globalFormat = null;
 
@@ -312,18 +319,22 @@
                         // 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,16 @@
     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;
+    }
 }
Index: core/src/main/java/org/apache/struts2/views/jsp/DateTag.java
===================================================================
--- core/src/main/java/org/apache/struts2/views/jsp/DateTag.java        (revision 613415)
+++ core/src/main/java/org/apache/struts2/views/jsp/DateTag.java        (working copy)
@@ -38,6 +38,7 @@
     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);
@@ -49,6 +50,7 @@
         d.setName(name);
         d.setFormat(format);
         d.setNice(nice);
+        d.setTimezone(timezone);
 
     }
 
@@ -63,4 +65,7 @@
     public void setName(String name) {
         this.name = name;
     }
+    public void setTimezone(String timezone) {
+        this.timezone = timezone;
+    }
 }
Index: core/src/site/resources/tags/date.html
===================================================================
--- core/src/site/resources/tags/date.html      (revision 613415)
+++ core/src/site/resources/tags/date.html      (working copy)
@@ -60,6 +60,14 @@
                                        <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>
                                        <td align="left" valign="top"></td>


> Add timezone support to date component
> --------------------------------------
>
>                 Key: WW-2326
>                 URL: https://issues.apache.org/struts/browse/WW-2326
>             Project: Struts 2
>          Issue Type: New Feature
>          Components: Plugin - Tags
>    Affects Versions: 2.1.1
>            Reporter: Serg Mazurok
>             Fix For: Future
>
>
> It would be nice to add timezone support to date component similar to fmt:formatDate.
> You  need to add 'timezone' parameter to Date tag and use it during formatting date.
> E.g. <s:date name="person.birthday" format="dd/MM/yyyy" timezone="session.timezone"/>

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.