Skip to content
Aasim's Web Corner
Aasim's Web Corner

Ink is better than the best memory.

  • Odysseys & Artistry
    • Saunterer Soul
    • My Poetry
    • My Sketch Work
  • Project Management
    • Agile & Frameworks
    • PMP Certification
  • Digital Diary
    • History
    • Islam
    • Life Around Us
    • My Bookshelf
  • My Tutorials
    • Amazon Kindle
    • Android
    • Aspect Oriented Programming
    • BlackBerry
    • Code Repositories
    • iOS
    • Java
    • JavaScript
    • Linux
    • Random Tips
Connect with me
Aasim's Web Corner

Ink is better than the best memory.

JavaDateFormatting-AasimNaseem.com

Date Formatting in Java

Aasim Naseem, August 13, 2010 | Read Count: 15,469May 5, 2025
Category: My Tutorials > Java

Hi 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.

free counters

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
  • Economic impact of Eid ul Adha - AasimNaseem.comIslamJune 6, 2025 | Read Count: 283Economic impact of Eid-ul-Adha
  • Best PMP Exam Study Resources - AasimNaseem.comPMP CertificationMay 23, 2025 | Read Count: 493Best PMP Study Resources for 2025 (Books, Courses, Tools & More)
  • agile vs scrum - AasimNaseem.comAgile & FrameworksMay 7, 2025 | Read Count: 463Agile vs Scrum: Finally Understanding the Difference
  • When Agile shouldn’t Use - AasimNaseem.comAgile & 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

Post navigation

Previous post
Next post

Related Posts

Java Java on Mac - AasimNaseem.com

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 - AasimNaseem.com

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 - AasimNaseem.com.jpg

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

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Economic impact of Eid-ul-Adha
    Economic impact of Eid-ul-Adha
    June 6, 2025 | Read Count: 283
  • Best PMP Study Resources for 2025 (Books, Courses, Tools & More)
    Best PMP Study Resources for 2025 (Books, Courses, Tools & More)
    May 23, 2025 | Read Count: 493
  • Agile vs Scrum: Finally Understanding the Difference
    Agile vs Scrum: Finally Understanding the Difference
    May 7, 2025 | Read Count: 463
  • When Not To Use Agile: 5 Signs You Need a Different Approach
    When Not To Use Agile: 5 Signs You Need a Different Approach
    April 25, 2025 | Read Count: 493
  • Quran on Peace and Kindness
    Quran on Peace and Kindness
    April 20, 2025 | Read Count: 452

Posts from Past

  • June 2025
  • May 2025
  • April 2025
  • January 2025
  • November 2024
  • April 2024
  • October 2022
  • August 2021
  • September 2020
  • May 2020
  • April 2019
  • January 2019
  • September 2018
  • July 2015
  • June 2015
  • November 2014
  • September 2014
  • April 2014
  • June 2013
  • May 2013
  • February 2013
  • January 2013
  • December 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • March 2012
  • February 2012
  • January 2012
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009

Categories List

  • Agile & Frameworks
  • Amazon Kindle
  • Android
  • Aspect Oriented Programming
  • BlackBerry
  • Blog
  • Code Repositories
  • History
  • iOS
  • Islam
  • Java
  • JavaScript
  • Life Around Us
  • Linux
  • My Bookshelf
  • My Poetry
  • My Sketch Work
  • PMP Certification
  • Random Tips
  • Saunterer Soul

Recent Posts

  • Economic impact of Eid-ul-Adha
  • Best PMP Study Resources for 2025 (Books, Courses, Tools & More)
  • Agile vs Scrum: Finally Understanding the Difference
  • When Not To Use Agile: 5 Signs You Need a Different Approach
  • Quran on Peace and Kindness

Recent Comments

  1. Aasim Naseem on When Not To Use Agile: 5 Signs You Need a Different Approach
  2. Aasim Naseem on When Not To Use Agile: 5 Signs You Need a Different Approach
  3. Masjid Wazir Khan, Lahore Pakistan - Aasim's Web Corner on Everlasting Art of Badshahi Masjid Lahore Pakistan
  4. Rishi Kumar on When Not To Use Agile: 5 Signs You Need a Different Approach
  5. Best PMP Study Resources for 2025 (Books, Courses, Tools & More) - Aasim's Web Corner on PMP Exam Eligibility 2025: 3 Things You Need to Know
©2025 Aasim's Web Corner | WordPress Theme by SuperbThemes