TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Ant Script to Change System Date-Time
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Linux Tutorials

Ant Script to Change System Date-Time

Last updated: Feb 25, 2024 10:54 am
By Harsh S.
Share
7 Min Read
Ant script to change system date and time on Windows and Linux.
Ant script to change system date and time.
SHARE

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 that can set both a past and future date-time with a unit factor that you specify.

Contents
Why Should You Use ANT Script?How to Download and Setup ANT Tool for ScriptingSample ANT Script to Change System Date and TimeCall the ANT Targets to Change the System Date-Time
Ant script to change system date and time.
Ant script to change system date and time.

Starting with Ant Script – Setup & Installation

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

Also Read: Learn to Create a Linux Service in 2 Minutes

Why Should You Use 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’s date and time.

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

How to Download and Setup ANT Tool for Scripting

We’ve compiled a few steps that you can follow to install and set up the ANT tool.

  • You can download ANT in compressed format from its official website.
  • Download and install Java Runtime Environment (JRE) from OpenJDK if not already installed.
  • Extract the ANT downloaded file into a directory.
  • Define the following environment variables.
JAVA_HOME (e.g. JAVA_HOME=C:\Program Files\Java\jdk1.7.0_51)
ANT_HOME to the directory you extracted ANT to
Add ${ANT_HOME}/bin (Unix) or %ANT_HOME%/bin (Windows) to your PATH.
  • 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.
ant -f fetch.xml -Ddest=system
  • 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:
C:\>ant -version
Apache Ant(TM) version 1.9.5 compiled on June 2 2015

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

Write Your First Ant Script – Example & Execution

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 set up ANT and then directly copy-paste the below code snippet into a file and save it as SetDateTime.xml.

<project name="SetDateTime" basedir=".">
	<!--Variables <=> Windows/Linux-->
	<property environment="env"/>
	<property name="workingDir" value="${env.WorkingDir}"/>
	<property name = "antLog" value = "antLog.log"/>
	<property name = "updateTime" value = "updateTime.properties"/>
	<condition property="format" value="dd-MM-yy">
		<os family="windows" />
	</condition>
	<condition property="format" value="YYYY-MM-dd">
		<os family="unix" />
	</condition>	

	<!--To Update Time-->
	<target name = "deleteUpdate">
		<delete file="${updateTime}" failonerror="false"/>
	</target>

	<!--Ant target to increase date -->
	<target name="increaseDate" depends="deleteUpdate">
		<delete file="${antLog}" failonerror="false"/>
		<propertyfile file="updateTime.properties">
			<entry  key="formated.tomorrow" type="date" default = "now" pattern="${format}" operation="+" value="${value}" unit="${unit}"/>
		</propertyfile>
		<property file="updateTime.properties"/>
		<exec executable="cmd" output="${antLog}" append="true" osfamily="windows">
			<arg value="/c"/>
			<arg value="date ${formated.tomorrow}"/>
		</exec>
		<exec executable="date" output="${antLog}" append="true" osfamily="unix">
			<arg value="-s"/>
			<arg value="${formated.tomorrow}"/>
		</exec>
	</target>

	<!--Ant target to decrease date -->
	<target name="decreaseDate" depends="deleteUpdate">
		<delete file="${antLog}" failonerror="false"/>
		<propertyfile file="updateTime.properties">
			<entry  key="formated.tomorrow" type="date" default = "now" pattern="${format}" operation="-" value="${value}" unit="${unit}"/>
		</propertyfile>
		<property file="updateTime.properties"/>
		<exec executable="cmd" output="${antLog}" append="true" osfamily="windows">
			<arg value="/c"/>
			<arg value="date ${formated.tomorrow}"/>
		</exec>
		<exec executable="date" output="${antLog}" append="true" osfamily="unix">
			<arg value="-s"/>
			<arg value="${formated.tomorrow}"/>
		</exec>
	</target>

	<!--Ant target to increase time -->
	<target name="increaseTime" depends="deleteUpdate">
		<delete file="${antLog}" failonerror="false"/>
		<propertyfile file="updateTime.properties">
			<entry key="futureTime" type="date" value="now" pattern="HH:mm"/>
			<entry key="futureTime" type="date" default="now" operation="+" value="${value}" unit="${unit}" pattern="HH:mm"/>
		</propertyfile>
		<property file="updateTime.properties"/>
		<exec executable="cmd" output="${antLog}" append="true" osfamily="windows">
			<arg value="/c"/>
			<arg value="time ${futureTime}"/>
		</exec>
		<exec executable="date" output="${antLog}" append="true" osfamily="unix">
			<arg value="-s"/>
			<arg value="${futureTime}"/>
		</exec>
	</target>

	<!--Ant target to decrease time -->
	<target name="decreaseTime" depends="deleteUpdate">
		<delete file="${antLog}" failonerror="false"/>
		<propertyfile file="updateTime.properties">
			<entry key="futureTime" type="date" value="now" pattern="HH:mm"/>
			<entry key="futureTime" type="date" default="now" operation="-" value="${value}" unit="${unit}" pattern="HH:mm"/>
		</propertyfile>
		<property file="updateTime.properties"/>
		<exec executable="cmd" output="${antLog}" append="true" osfamily="windows">
			<arg value="/c"/>
			<arg value="time ${futureTime}"/>
		</exec>
		<exec executable="date" output="${antLog}" append="true" osfamily="unix">
			<arg value="-s"/>
			<arg value="${futureTime}"/>
		</exec>
	</target>
</project>

Call the ANT Targets to Change the System Date-Time

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

  • Set Java/JDK environment variables like JAVA_HOME, JDK_HOME, etc.
  • Define the ANT variables like ANT_HOME to set the ANT path.
  • Save the code given in the previous section as SetDateTime.xml.

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

Example-1) Increase the Date by 2 days.

ant.bat -Dunit=day -Dvalue=2 -f SetDateTime.xml increaseDate

Example-2) Decrease the Date by 2 days.

ant.bat -Dunit=day -Dvalue=2 -f SetDateTime.xml DecreaseDate

Example-3) Increase Time by 2 hours.

ant.bat -Dunit=hour -Dvalue=2 -f SetDateTime.xml increaseTime

Example-4) Decrease Time by 2 hours.

ant.bat -Dunit=hour -Dvalue=2 -f SetDateTime.xml DecreaseTime

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 units in day or hour and value is the factor by which you would like the date/time to increase/decrease.

Final Thought

We wish that while reading this article, you would have created a basic understanding of 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 input will keep us motivated and drive us to produce improved content every time.

Keep Learning,

TechBeamers.

You Might Also Like

Basic Linux Commands for Beginners With Examples

How to Use Bash to Replace Character in String

Simple Bash Scripting Tutorial for Beginners

20 Bash Script Code Challenges for Beginners with Their Solutions

Difference Between Git Repository and Directory

Harsh S. Avatar
By Harsh S.
Follow:
Hello, I'm Harsh, I hold a degree in Masters of Computer Applications. I have worked in different IT companies as a development lead on many large-scale projects. My skills include coding in multiple programming languages, application development, unit testing, automation, supporting CI/CD, and doing DevOps. I value Knowledge sharing and want to help others with my tutorials, quizzes, and exercises. I love to read about emerging technologies like AI and Data Science.
Previous Article GDB Tutorial - Advanced Debugging Tips GDB Tutorial: Some Cool Tips to Debug C/C++ Code
Next Article C Programming Tips and Tricks. C Programming Tips and Tricks for Beginners

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x