Add a timestamp to a Java application

Often in the “about” section of an application, the version number is reported. It is a good idea to add to the version number also a timestamp that states when the app was built.

There is an easy automatic way to do that in a Java application built with Netbeans.

What we need to do first is to prepare a template file called We keep it in a directory called “template”, which is at the same level as the “src” subdirectory in the project. (it could be in any other place, and have any other name, but here we will assume these values. If we want to change them we need to modify the script – see below).

The content of the file will be:

package myPackage;
public class BuildTime {
   public static String build_time = "@DSTAMP@-@TSTAMP@";

The placeholders @DSTAMP@ and @TSTAMP@ will be updated by Netbeans when we compile the code of our application.

Netbeans uses an ant script to perform its various operations (clean, compile, debug, run…). The script is in the file build-impl.xml in the nbproject subdirectory in our project. Let us see now how to modify the script so that it will automatically generate a timestamp. In the build-impl.xml file there is a section with the target pre-compile, which is executed every time before compiling the project. The standard content of the section is as follows:

<target name="-pre-compile">
<!-- Empty placeholder for easier customization. -->
<!-- You can override this target in the ../build.xml file. -->

We modify it in the following way:

<target name="-pre-compile">
   <touch file="template/" />
   <copy file="template/" toFile="src/myPackage/">
         <filter token="DSTAMP" value="${DSTAMP}"/>
         <filter token="TSTAMP" value="${TSTAMP}"/>

Right before compilation (at pre-compile time) the script will generate a timestamp, and then it will copy our template file into src/myPackage, changing the placeholders @DSTAMP@ and @TSTAMP@ with the actual timestamp values.

Having done this, the static String BuildTime.build_time will contain our compilation time stamp. We can use it in our code wherever we like, as in the following example:

package myPackage;
public class Demo  {
   public static void main (String a[]) {
      System.out.println("Hello world! This application was generated on "+BuildTime.build_time);>

Of course, we will need to customize the name of the package to suit our needs. It is important to observe that:

  • The original copy of our template file should be kept in the “template” directory (we can of course change the name of the file and of the directory if we want by modifying the script)
  • The copy of the file in /myPackage should not be manually modified, as it is automatically regenerated (with the inclusion of the actual timestamp) when a build of the program is performed.
  • The pre-compile target (and hence the script) will be invoked when we  do “Build Main Project or “Clean and Build Main Project”, but not when we do a “Run Main Project, even if that  recompiles some files!
Posted in Java, Programming

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: