How to Calculate Time difference in ANT Script
We can calculate time difference in ANT script using java script. The follow sample script will give you the ANT target execution time.
<?xml version="1.0"?>
<project basedir="." name="24x7">
<target name="run24x7">
<taskdef resource="net/sf/antcontrib/antcontrib.properties" />
<script language="javascript">
<![CDATA[
importClass(java.text.SimpleDateFormat);
fmt = new SimpleDateFormat("MM/dd/YYYY HH:mm:ss a");
self.getProject().setProperty('startdateformat', fmt.format(new Date().getTime()));
project.setProperty('startTime', new Date().getTime());
]]>
</script>
<echo> time is ${startdateformat}</echo>
<echo> time is ${startTime}</echo>
<parallel threadcount="3">
<echo message="UseCase1" />
<echo message="UseCase2" />
<echo message="UseCase3" />
</parallel>
<script language="javascript">
project.setProperty('endTime', new Date().getTime())
<![CDATA[
importClass(java.text.SimpleDateFormat);
fmt = new SimpleDateFormat("MM/dd/YYYY HH:mm:ss a");
self.getProject().setProperty('enddateformat', fmt.format(new Date().getTime()));
project.setProperty('elapsedTime', new Date().getTime() - startTime) ]]> </script>
<echo>End Date format : ${enddateformat} ms</echo>
<echo>End time: ${endTime} ms</echo>
<echo>Elapsed time: ${elapsedTime} ms</echo>
<math result="result" operand1="${elapsedTime}" operation="/" operand2="1000" datatype="double"/>
<echo>result is ${result}</echo>
</target>
</project>
<project basedir="." name="24x7">
<target name="run24x7">
<taskdef resource="net/sf/antcontrib/antcontrib.properties" />
<script language="javascript">
<![CDATA[
importClass(java.text.SimpleDateFormat);
fmt = new SimpleDateFormat("MM/dd/YYYY HH:mm:ss a");
self.getProject().setProperty('startdateformat', fmt.format(new Date().getTime()));
project.setProperty('startTime', new Date().getTime());
]]>
</script>
<echo> time is ${startdateformat}</echo>
<echo> time is ${startTime}</echo>
<parallel threadcount="3">
<echo message="UseCase1" />
<echo message="UseCase2" />
<echo message="UseCase3" />
</parallel>
<script language="javascript">
project.setProperty('endTime', new Date().getTime())
<![CDATA[
importClass(java.text.SimpleDateFormat);
fmt = new SimpleDateFormat("MM/dd/YYYY HH:mm:ss a");
self.getProject().setProperty('enddateformat', fmt.format(new Date().getTime()));
project.setProperty('elapsedTime', new Date().getTime() - startTime) ]]> </script>
<echo>End Date format : ${enddateformat} ms</echo>
<echo>End time: ${endTime} ms</echo>
<echo>Elapsed time: ${elapsedTime} ms</echo>
<math result="result" operand1="${elapsedTime}" operation="/" operand2="1000" datatype="double"/>
<echo>result is ${result}</echo>
</target>
</project>