Pages

Friday, December 3, 2021

Java Calendar Add

1. Introduction

In this tutorial, we'll learn how to add or subtract the time units from the Calendar object in java.

java.util.Calendar.add() method is used to increase or reduce the specified amount of time to the given calendar field.


Java Calendar Add



2. java.util.Calendar.add() Syntax


Below is the syntax of add() method.

public abstract void add(int field, int amount)

For example, if you want to add 5 days to the current calendar time then you need to do the following to achieve this.

add(Calendar.DAY_OF_MONTH, -5).

Calendar class is added with all fields of date related. You can use them as Calendar.DAY, Calendar.MONTH and Calendar.YEAR etc.


3. Java Calendar Add Examples - 1


We will write the examples to work with the calendar object to go to the future date or past date using add() method.

Example 1
package com.javaprogramto.java8.dates.calendar;

import java.util.Calendar;

public class CalendarAddExample1 {

	public static void main(String[] args) {
		// current calendar date
		Calendar calendar = Calendar.getInstance();

		System.out.println("Current time - " + calendar.getTime());

		// Adding 10 days to get the future date
		calendar.add(Calendar.DATE, 10);
		System.out.println("10 days future time - " + calendar.getTime());

		// 10 days old date to get the past date
		calendar.add(Calendar.DATE, -10);
		System.out.println("10 days past time - " + calendar.getTime());

		// future 10 years
		calendar.add(Calendar.YEAR, 10);
		System.out.println("10 years future time - " + calendar.getTime());

		// past 10 years
		calendar.add(Calendar.YEAR, -10);
		System.out.println("10 years back time - " + calendar.getTime());

		// future 10 months
		calendar.add(Calendar.MONTH, 10);
		System.out.println("10 months future time - " + calendar.getTime());

		// past 10 months
		calendar.add(Calendar.MONTH, -10);
		System.out.println("10 months back time - " + calendar.getTime());

	}

}

Output
Current time - Fri Dec 03 16:52:20 IST 2021
10 days future time - Mon Dec 13 16:52:20 IST 2021
10 days past time - Fri Dec 03 16:52:20 IST 2021
10 years future time - Wed Dec 03 16:52:20 IST 2031
10 years back time - Fri Dec 03 16:52:20 IST 2021
10 months future time - Mon Oct 03 16:52:20 IST 2022
10 months back time - Fri Dec 03 16:52:20 IST 2021

4. Java Calendar Add Examples - 2


Example programs to use Calendar.HOUR and Calendar.MINUTE.

Example 2
package com.javaprogramto.java8.dates.calendar;

import java.util.Calendar;

public class CalendarAddExample2 {

	public static void main(String[] args) {
		// current calendar date
		Calendar calendar = Calendar.getInstance();

		System.out.println("Current time - " + calendar.getTime());

		calendar.add(Calendar.HOUR, 1);
		System.out.println("adding one hour" + calendar.getTime());

		calendar.add(Calendar.HOUR, -1);
		System.out.println("one hour back time - " + calendar.getTime());

		// future 10 minutes
		calendar.add(Calendar.MINUTE, 10);
		System.out.println("10 minutes future time - " + calendar.getTime());

		// past 10 minutes
		calendar.add(Calendar.MINUTE, -10);
		System.out.println("10 minutes back time - " + calendar.getTime());

	}

}

Output
Current time - Fri Dec 03 16:59:13 IST 2021
adding one hourFri Dec 03 17:59:13 IST 2021
one hour back time - Fri Dec 03 16:59:13 IST 2021
10 minutes future time - Fri Dec 03 17:09:13 IST 2021
10 minutes back time - Fri Dec 03 16:59:13 IST 2021


5. Conclusion


In this article, We've seen how to add or remove some amount of time for the given date-time field.




No comments:

Post a Comment

Please do not add any spam links in the comments section.