javadateantpropertiestimestamp

Ant: How can I subtract two properties (containing timestamps)?


I'm working on an ant script. In this particular part, I need to get the current month, as well as the prior month. I was thinking something similar to

<tstamp>
   <format property="thismonth" pattern="MMyy"/> <!-- 0210 by february 2010-->
</tstamp>

<!--I'd like to get 0110 (january 2010) here, but can't imagine how-->
<property name="priormonth" value="?">

I've been reading on property helpers, but I can't get what I need. What can I try next?


Solution

  • You can do it with a custom JavaScript scriptdef:

    <project default="build">
    
        <target name="build">
            <echo message="Hello world"/>
            <setdates/>
            <echo message="thismonth ${thismonth}"/>
            <echo message="priormonth ${priormonth}"/>
        </target>
    
        <scriptdef name="setdates" language="javascript">
            <![CDATA[
    
                importClass(java.text.SimpleDateFormat);
                importClass(java.util.Calendar);
    
                today = new Date();
    
                cal = Calendar.getInstance();
                cal.setTime(today);
                cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
    
                priormonth = cal.getTime();
    
                fmt = new SimpleDateFormat("MMyy");
    
                self.getProject().setProperty('thismonth', fmt.format(today));
                self.getProject().setProperty('priormonth', fmt.format(priormonth));
    
            ]]>
        </scriptdef>
    
    </project>