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

I am working on an ant script. In this particular part, I need to get the current month, as well as the previous month. I thought 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 read about property assistants, but I cannot get what I need. Any ideas?

Thanks in advance.

+3
source share
4 answers

You can do this with a special 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>
+4
source

I'm sure some regex may surprise, but I'm just creating a custom task.

getProjet().setProperty().

:

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);
    }

}

, :

previousmonth = org.myproject.PreviousMonthTask

(. Ant), :

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

ANT tstamp :

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

. , , , , , .

+2

:

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

, 2 :

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

Source: https://habr.com/ru/post/1732231/


All Articles