conor 01/01/05 01:45:47
Modified: src/main/org/apache/tools/ant/taskdefs Tstamp.java
Log:
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 <[EMAIL PROTECTED]>
Revision Changes Path
1.9 +44 -0
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Tstamp.java
Index: Tstamp.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Tstamp.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- Tstamp.java 2001/01/04 11:13:17 1.8
+++ Tstamp.java 2001/01/05 09:45:47 1.9
@@ -65,6 +65,7 @@
* @author [EMAIL PROTECTED]
* @author [EMAIL PROTECTED]
* @author [EMAIL PROTECTED]
+ * @author [EMAIL PROTECTED]
*/
public class Tstamp extends Task {
@@ -106,6 +107,8 @@
{
private String propertyName;
private String pattern;
+ private int offset = 0;
+ private int field = Calendar.DATE;
public CustomFormat()
{
@@ -121,6 +124,40 @@
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 @@
}
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));
}
}