Date Formatting in Java Aasim Naseem, August 13, 2010 | Read Count: 15,469May 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: 283Economic impact of Eid-ul-Adha PMP CertificationMay 23, 2025 | Read Count: 493Best PMP Study Resources for 2025 (Books, Courses, Tools & More) Agile & FrameworksMay 7, 2025 | Read Count: 463Agile vs Scrum: Finally Understanding the Difference Agile & FrameworksApril 25, 2025 | Read Count: 493When Not To Use Agile: 5 Signs You Need a Different Approach Java Calendar JavaDate Formatting in C#Date Formatting in JavaDateFormat
Java Java on Mac February 17, 2011 | Read Count: 14,454May 5, 2025 Category: My Tutorials > JavaHello Guys; Hows everything with you? Today I was planning to write the next part of my Amazon Kindle application development tutorial. Nowadays I’m using a MacBook, so I thought I needed to download and install JDK first. But surprisingly, I came to know every version… Read More
Java Java 1.7 G1 Garbage Collector November 3, 2011 | Read Count: 14,496May 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 Book Review :: Java/J2EE Job Interview Companion May 13, 2010 | Read Count: 15,494May 5, 2025 Category: My Tutorials > JavaJava/J2EE Job Interview Companion is book having 400+ Java/J2EE Interview questions with clear and concise answers for: job seekers (junior/senior developers, architects, team/technical leads), promotion seekers, pro-activelearners and interviewers. Free download Lulu top 100 best seller. Increase your earning potential by learning, applying and succeeding. Learn… Read More