Ant – How to echo current time and system date in Ant

Ant has task Tstamp which easily get the current date or system time with TODAY property, the output date can be formatted in any particular custom date/time patterns which are defined in the Java SimpleDateFormat class.

The example to echo current date in Ant:

<tstamp>
  <format property="TODAY_UK" pattern="yyyy-mmmm-dd" locale="en,UK"/>
</tstamp>
<echo>the current date is ${TODAY_UK}</echo>

It set property TODAY_UK with that the date is today and pattern “yyyy-mmmm-dd” using English locale. then echo the value, note locale specifics the timezone to format for displaying time.

The example to echo current time in Ant:

<tstamp>
  <format property="TODAY_UK" pattern="HH:mm:ss:sss zzz" locale="en,UK"/>
</tstamp>
<echo>the current time is ${TODAY_UK}</echo>

We used a different time pattern to get current time up to milliseconds.

The entire ant script XML to get system date and time:

Build.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="echotime" name="Echo system date/time">
  <target name="echotime">
    <tstamp>
      <format property="TODAY_UK" pattern="yyyy-MM-dd HH:mm:ss:sss zzz" 
 locale="cn,CN"/>
    </tstamp>
    <echo>the system date/time is ${TODAY_UK}</echo>
  </target>
</project>

The output was

Buildfile: build.xml

echotime:
[echo] the current time is 2012-12-12 15:41:33:033 GMT

BUILD SUCCESSFUL

Оцените статью
ASJAVA.COM
Добавить комментарий

Your email address will not be published. Required fields are marked *

*

code

  1. Dan

    Thanks, works great!

    One mention, though, specifying the locale made no difference for me (ant 1.8.4).

    Ответить