开发者

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="?">
开发者_如何学C

I've been reading on property helpers, but I cant get what I need. Any ideas?

Thanks in advance.


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>


I'm sure some regex can do wonder but I would simply create a custom Task.

Within your task, you can define a new property with the getProjet().setProperty() method.

Something like the following should suffice:

public class PreviousMonthTask extends Task {

    private String currentDate;
    private String propertyName;

    public void setCurrentDate(String currentDate) {
        this.currentDate = currentDate;
    }

    public void setPropertyName(String propertyName) {
        this.propertyName = propertyName;
    }

    @Override
    public void execute() throws BuildException {
        // calculate the previous month
        String previousMonth = ...;
        getProject().setProperty(this.propertyName, previousMonth);
    }

}

What's left to do is to define a properties file with a link to the class:

previousmonth = org.myproject.PreviousMonthTask

When you load the task (see the Ant documentation), you just have to invoke your task with:

<previousmonth propertyName="previous" currentDate="${current}"/>


ANT's tstamp task has an offset element:

<tstamp>
    <format property="twoDaysAgo" pattern="yyyy-MM-dd" offset="-2"/>
</tstamp>

This returns me a timestamp for two days ago. I would expect you'd be able to do the same thing if your pattern is months, then the offset would probably work in months.


Actually you can use:

<tstamp>
    <format property="twoDaysAgo" pattern="yyyy-MM-dd" unit="day" offset="-2"/>
</tstamp>

So for 2 prior months you use:

<tstamp>
   <format property="twoDaysAgo" pattern="yyyy-MM-dd" unit="month" offset="-2"/>
</tstamp>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜