Date Formatting in Java Aasim Naseem, August 13, 2010 | Read Count: 15,471May 5, 2025 Category: My Tutorials > JavaHi all… Hope you’re enjoying your time… Today I will explain how to use date-time (date manipulation) in the Java programming language. I was working on a task where I needed to play with time/date. In my other post, I explained the date formatting in C#. Today you will learn the same concept in Java. The 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 and time with the Calendar class. First get the current date and 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 Java import 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())); } } Output Current 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 Thanks for your time. Author Profile Aasim Naseem Hey, Thanks for your interest. I’m a PMP, AWS Solutions Architect, and Scrum Master certified professional with 17+ years of hands-on experience leading projects, building teams, and helping organizations deliver software solutions better, faster, and smarter. Outside of work, I’ve got a deep curiosity for history — especially ancient civilizations like Egypt. I also enjoy reflecting on the everyday moments that shape how we live and work. This blog is my space to share insights, lessons, and thoughts from both my professional journey and personal interests. Thanks for reading — and I hope you will find something here that matches your interest. Latest entries IslamJune 6, 2025 | Read Count: 292Economic impact of Eid-ul-Adha PMP CertificationMay 23, 2025 | Read Count: 497Best PMP Study Resources for 2025 (Books, Courses, Tools & More) Agile & FrameworksMay 7, 2025 | Read Count: 472Agile vs Scrum: Finally Understanding the Difference Agile & FrameworksApril 25, 2025 | Read Count: 503When Not To Use Agile: 5 Signs You Need a Different Approach Java Calendar JavaDate Formatting in C#Date Formatting in JavaDateFormat
Java Java Sorting – Comparator vs Comparable September 20, 2010 | Read Count: 15,500May 5, 2025 Category: My Tutorials > JavaHello All … I hope everyone is in good health and enjoying Allah Almighty’s blessings. I was working on a task where I needed to sort a collection (list) of objects on the basis of some attribute within a single element of that list. Though I… Read More
Java Java 1.7 G1 Garbage Collector November 3, 2011 | Read Count: 14,502May 5, 2025 Category: My Tutorials > JavaHi all, Hope everything is going good at your desks; Last night, I was reading new features in Java 1.7 and found some updates in garbage collection mechanism; There are many updates in 1.7 ranges from core vm, i18n, Nimbus for Swings and other enhancements. We… Read More
Java Eclipse on Mac February 18, 2011 | Read Count: 14,419May 5, 2025 Category: My Tutorials > JavaAnd finally, I’m able to run Eclipse on Mac. And now i’m seriously thinking of having macbook permanently. As i can taste sweetness of xcode apps along with coffee cup of java, Yeah, i will miss the tag team of C# and ASP.NET. Author Profile Aasim… Read More