Browse Source

Add the ability to specify time offsets when generating time/date property

strings.

Example usage:

  <target name="dateformats">
    <tstamp>
      <format property="now" pattern="dd/MM/yyyy HH:mm:ss.SS"/>
      <format property="then" offset="-1" unit="year"
              pattern="dd/MM/yyyy HH:mm:ss.SS"/>
    </tstamp>
    <echo message="now = ${now}, then = ${then}"/>
  </target>

Supported units are millisecond, second, minute, hour, day, week, month,
year. The unit is case insensitive

Based on the suggestion of Thomas Christen <chr@active.ch>


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268405 13f79535-47bb-0310-9956-ffa450edef68
master
Conor MacNeill 24 years ago
parent
commit
e48fc02b3f
1 changed files with 44 additions and 0 deletions
  1. +44
    -0
      src/main/org/apache/tools/ant/taskdefs/Tstamp.java

+ 44
- 0
src/main/org/apache/tools/ant/taskdefs/Tstamp.java View File

@@ -65,6 +65,7 @@ import java.text.*;
* @author costin@dnt.ro
* @author stefano@apache.org
* @author roxspring@yahoo.com
* @author conor@cognet.com.au
*/
public class Tstamp extends Task {
@@ -106,6 +107,8 @@ public class Tstamp extends Task {
{
private String propertyName;
private String pattern;
private int offset = 0;
private int field = Calendar.DATE;
public CustomFormat()
{
@@ -121,6 +124,40 @@ public class Tstamp extends Task {
this.pattern = pattern;
}
public void setOffset(int offset) {
this.offset = offset;
}
public void setUnit(String unit) {
if (unit.equalsIgnoreCase("millisecond")) {
field = Calendar.MILLISECOND;
}
else if (unit.equalsIgnoreCase("second")) {
field = Calendar.SECOND;
}
else if (unit.equalsIgnoreCase("minute")) {
field = Calendar.MINUTE;
}
else if (unit.equalsIgnoreCase("hour")) {
field = Calendar.HOUR_OF_DAY;
}
else if (unit.equalsIgnoreCase("day")) {
field = Calendar.DATE;
}
else if (unit.equalsIgnoreCase("week")) {
field = Calendar.WEEK_OF_YEAR;
}
else if (unit.equalsIgnoreCase("month")) {
field = Calendar.MONTH;
}
else if (unit.equalsIgnoreCase("year")) {
field = Calendar.YEAR;
}
else {
throw new BuildException(unit + " is not a unit supported by the tstamp task");
}
}
public void execute(Project project, Date date)
{
if (propertyName == null) {
@@ -132,6 +169,13 @@ public class Tstamp extends Task {
}
SimpleDateFormat sdf = new SimpleDateFormat (pattern);
if (offset != 0) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(field, offset);
date = calendar.getTime();
}

project.setProperty(propertyName, sdf.format(date));
}
}


Loading…
Cancel
Save