Date Formatting in Java Aasim Naseem, August 13, 2010March 18, 2024 Hi All ..hope enjoying time ..today i will explain how to use date time (Date Manipulation) in java programming language. I was working on a task where i need to play with time/date. In my other post i explained Date Formatting in C#. Today you will learn same concept in java.Java Calendar class (java.util.Calendar) is a very useful and handy class in java date time manipulation. here i will demonstrate how to modify date time with calender class. First get current date time with Calendar()DateFormat dateFormat = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”); Calendar cal = Calendar.getInstance(); System.out.println(“Current Date Time : ” + dateFormat.format(cal.getTime()));now you can use following calender date time manipulation function as per your need//Add one day to current date time cal.add(Calendar.DATE, 1);//Add one month to current date time cal.add(Calendar.MONTH, 1);//Add one year to current date time cal.add(Calendar.YEAR, 1);//Add one hour to current date time cal.add(Calendar.HOUR, 1);//Add one minute to current date time cal.add(Calendar.MINUTE, 1);//Add one second to current date time cal.add(Calendar.SECOND, 1);//Subtract one day from current date cal.add(Calendar.DATE, -1);//Subtract one month from current date cal.add(Calendar.MONTH, -1);//Subtract one year from current date cal.add(Calendar.YEAR, -1);//Subtract one hour from current date cal.add(Calendar.HOUR, -1);//Subtract one minute from current date cal.add(Calendar.MINUTE, -1);//Subtract one second from current date cal.add(Calendar.SECOND, -1);Here is the full source code to show how to modify date time in Javaimport java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar;public class DateTimeManipulation { public static void main(String[] args) {DateFormat dateFormat = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”); //get current date time with Calendar() Calendar cal = Calendar.getInstance(); System.out.println(“Current Date Time : ” + dateFormat.format(cal.getTime()));cal.add(Calendar.DATE, 1); System.out.println(“Add one day to current date : ” + dateFormat.format(cal.getTime()));cal = Calendar.getInstance(); cal.add(Calendar.MONTH, 1); System.out.println(“Add one month to current date : ” + dateFormat.format(cal.getTime()));cal = Calendar.getInstance(); cal.add(Calendar.YEAR, 1); System.out.println(“Add one year to current date : ” + dateFormat.format(cal.getTime()));cal = Calendar.getInstance(); cal.add(Calendar.HOUR, 1); System.out.println(“Add one hour to current date : ” + dateFormat.format(cal.getTime()));cal = Calendar.getInstance(); cal.add(Calendar.MINUTE, 1); System.out.println(“Add one minute to current date : ” + dateFormat.format(cal.getTime()));cal = Calendar.getInstance(); cal.add(Calendar.SECOND, 1); System.out.println(“Add one second to current date : ” + dateFormat.format(cal.getTime()));cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); System.out.println(“Subtract one day from current date : ” + dateFormat.format(cal.getTime()));cal = Calendar.getInstance(); cal.add(Calendar.MONTH, -1); System.out.println(“Subtract one month from current date : ” + dateFormat.format(cal.getTime()));cal = Calendar.getInstance(); cal.add(Calendar.YEAR, -1); System.out.println(“Subtract one year from current date : ” + dateFormat.format(cal.getTime()));cal = Calendar.getInstance(); cal.add(Calendar.HOUR, -1); System.out.println(“Subtract one hour from current date : ” + dateFormat.format(cal.getTime()));cal = Calendar.getInstance(); cal.add(Calendar.MINUTE, -1); System.out.println(“Subtract one minute from current date : ” + dateFormat.format(cal.getTime()));cal = Calendar.getInstance(); cal.add(Calendar.SECOND, -1); System.out.println(“Subtract one second from current date : ” + dateFormat.format(cal.getTime()));} }OutputCurrent Date Time : 2010/08/13 10:24:53 Add one day to current date : 2010/08/14 10:24:53 Add one month to current date : 2010/09/13 10:24:53 Add one year to current date : 2011/08/13 10:24:53 Add one hour to current date : 2010/08/13 11:24:53 Add one minute to current date : 2010/08/13 10:25:53 Add one second to current date : 2010/08/13 10:24:54 Subtract one day from current date : 2010/08/12 10:24:53 Subtract one month from current date : 2010/07/13 10:24:53 Subtract one year from current date : 2009/08/13 10:24:53 Subtract one hour from current date : 2010/08/13 09:24:53 Subtract one minute from current date : 2010/08/13 10:23:53 Subtract one second from current date : 2010/08/13 10:24:52 Aasim NaseemI’m an engineer by profession, a blogger and a photojournalist by hobby. Seasonal writer at LAFZ Media UK. I write on different topics and things around us. Contact me at +971.56.126.8842 or Aasim.Naseem@outlook.com General / Others Tips & Tutorials Calendar JavaDate Formatting in C#Date Formatting in JavaDateFormat