java.util.Calendar add(int field,int amount)

Description :

This java tutorial shows how to use the add(int field,int amount) method of Calendar class of java.util package. This method adds or subtracts the specified amount of time to the given calendar field, based on the calendar’s rules.

Method Syntax :

public abstract void add(int field,int amount)

Parameter Input :

 

DataType Parameter Description
int field the calendar field
int amount the amount of date or time to be added to the field.

 

Method Returns :

This method returns void.

Compatibility Version :

Requires Java 1.1 and up

Exception :

N/A

Java Code Example :

This java example source code demonstrates the use of add(int field,int amount)  method of Calendar class. Basically on this example we just add and subtract values on different Calendar fields.

package com.javatutorialhq.java.examples;

import java.util.Calendar;

/*
 * This example source code demonstrates the use of  
 * add(int field,int amount) method of Calendar class
 */

public class CalendarAddExample {

	public static void main(String[] args) {

		Calendar cal = Calendar.getInstance();
		System.out.println("Time:" + cal.getTime());
		cal.add(Calendar.YEAR, 9);
		System.out.println("Time after adding year:" + cal.getTime());
		cal.add(Calendar.HOUR, -7);
		System.out.println("Time after subtracting hour:" + cal.getTime());

	}
}

Sample Output :

Running the add(int field,int amount) method example source code of Calendar class will give you the following output:

Time:Sun Feb 08 02:20:57 CST 2015
Time after adding year:Thu Feb 08 02:20:57 CST 2024
Time after subtracting hour:Wed Feb 07 19:20:57 CST 2024

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :