Search code examples
jspstruts2ognlstruts-tagsvaluestack

Struts2 taglib dates calculation remove decimals


I'm using Struts2 taglib to generate a list of descending years (2016, 2015, etc..).

I'm using the date tag to get year from a java.util.Date, however when I use minus (-) operator year comes correct but with decimals...

Take a look:

<s:set var="currentDate" value="%{new java.util.Date()}"/>
<s:date var="currentDate2YEAR" name="#currentDate" format="yyyy" />
<br>(YEAR)  = <s:property value="#currentDate2YEAR" />
<br>(YEAR -1)  = <s:property value="#currentDate2YEAR - 1" />

With this I get:

(YEAR) = 2016

(YEAR -1) = 2015.0     <== I need to remove this decimal ==>

Solution

  • I've found this solution and it works (it uses the intValue method):

    <s:date var="currentDate2YEAR" name="#currentDate" format="yyyy" />
    <br>(YEAR)  = <s:property value="#currentDate2YEAR" />
    <br>(YEAR -1)  = <s:set var="year" value="#currentDate2YEAR - 1" />
    <br>(YEAR -1) = <s:property value="#year.intValue()" />