You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Christian Bockermann <ch...@dorf.de> on 2009/10/15 22:03:44 UTC

Using date to format a date

Hi list!

I want to display a date in a user-specified manner (struts2 web-app)
Thus, there exists a form where users can specify date-format strings,  
I validate them
and on another page, I want to use the date-tag to display the date in  
the choosen format.

So I have a session-property "date-format", accessible with

	<s:property value="#session.date-format" />

and would like to use this format property, something like

	<s:date name="date" format="#session.date-format" />

Unfortunately the date-tag does not allow expressions in its 'format'  
attribute. Any
way around? Or is this simply not the right way to solve such a thing?

Best regards,

    Chris

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


Re: Using date to format a date

Posted by Greg Lindholm <gr...@gmail.com>.
What I do is add a method for formatting dates (formatDateTime()) to the
action base class.  The method can then use the users Locale and TimeZone to
construct a DateFormat object.

    public String formatDateTime(Date timestamp)
    {
        return getDateFormat().format(timestamp);
    }

In the jsp I then I can then format the date ('updated' in this case) like
this:

<s:property value="formatDateTime(updated)" />

If you already have a DateFormat object around you could create a method in
you action that takes both the Date and the DateFormat.

Note: Be careful storing DateFormat in the Session as DateFormat is not
synchronized so it can not used concurrently by multiple threads.


On Thu, Oct 15, 2009 at 4:03 PM, Christian Bockermann <ch...@dorf.de> wrote:

> Hi list!
>
> I want to display a date in a user-specified manner (struts2 web-app)
> Thus, there exists a form where users can specify date-format strings, I
> validate them
> and on another page, I want to use the date-tag to display the date in the
> choosen format.
>
> So I have a session-property "date-format", accessible with
>
>        <s:property value="#session.date-format" />
>
> and would like to use this format property, something like
>
>        <s:date name="date" format="#session.date-format" />
>
> Unfortunately the date-tag does not allow expressions in its 'format'
> attribute. Any
> way around? Or is this simply not the right way to solve such a thing?
>
> Best regards,
>
>   Chris
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>