Ant Script to Change System Date-Time on Win/Linux/Mac OS

Probably you are looking for a script that can change (increase/decrease) system date and time on Windows and Linux platforms. In this post, we’ll be exactly doing that. We’ll tell you how to create an ANT script which can set both past and a future date and time by a unit factor that you can specify.

This script is especially useful for automation testing, build infra operations, and in executing DevOps tasks. It’s because you need to support multiple platforms for all these requirements. And many times it requires to play with the system date/time for a quick execution.

1- Starting with Ant Script – Setup & Installation.

Ant script to change system date and time.

Ant script to change system date and time.

1.1- What is ANT Script?

First of all, you should know that ANT is a universal build tool created in Java. It supports multiple platforms like Windows, Linux, and Mac OS X. It operates using a build rule file in XML format. In this file, you can define all the build logic and add build dependencies. It primarily supports building large Java projects with dependencies. You can use this tool for extended usage like the ones we’ve mentioned in the beginning. In this blog post, we’ll teach you how to create an ANT script to change the system date and time.

Let’s first start with the ANT installation and setup.

1.2- How to Download and Setup ANT Tool for Scripting.

We’ve compiled few steps that you can follow to install and setup ANT tool.

1.2.1- You can download ANT in compressed format from its official website.
1.2.2- Download and install Java Runtime environment (JRE) from OpenJDK if not already installed.
1.2.3. Extract the ANT downloaded file into a directory.
1.2.4- Define the following environment variables.

1.2.5- Next update the library dependencies of ANT tasks by running the following command from the ANT_HOME folder:

If you don’t perform this, some of the dependent Ant tasks will not be available.

1.2.6- This is how you can check the Apache ANT Installation.

To confirm the successful ANT installation on your computer, type ant onto the console prompt.
You should see an output similar to:

If you do not see the similar output, then please revisit to check that you have performed the installation instructions properly.

2- Write Your First Ant Script – Example & Execution.

2.1- Sample ANT script to change system date and time.

Dear readers, in this section I’ve chipped in the full ready to use ANT script sample for your reference. Follow all the steps to setup ANT and then directly copy-paste the below code snippet into a file and save it as <SetDateTime.xml>.


2.2- How to call the ANT targets to change system date/time.

Calling the ANT target is pretty simple. But before you run the ANT script, make sure the following.

2.2.1- Set Java/JDK environment variables like JAVA_HOME, JDK_HOME etc.

2.2.2- Define the ANT variables like ANT_HOME to set the ANT path.

2.2.2- Save the code given the previous section as <SetDateTime.xml>.

In the next few lines, you will the find the desired commands to invoke the ANT targets.

Example-1) Increase Date by 2 days.

Example-2) Decrease Date by 2 days.

Example-3) Increase Time by 2 hours.

Example-4) Decrease Time by 2 hours.

So from the above examples, you can observe the ANT script’s (ant.bat) command line input parameters. There are two main parameters i.e. “unit” and “value“. These parameters accept date/time unit in day or hour and value is the factor which you would like the date/time to increase/decrease by.


Final Thought.

We wish that while reading this article, you would have created a basic understanding about the ANT script. And probably you all would be using this information in your live test automation or build infra environments.

Finally, we would like to request you to leave your feedback/comments in the comment section. Your inputs will keep us motivated and drive us to produce improved contents every time.


Learn to Succeed,


Leave a Reply