Calendar add(int, int) in Java

add(int, int): This method is available in java.util.Calendar class of Java.

Syntax:

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

This method takes two arguments of type int as its parameter. This method adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.

Parameters: Two parameters are required for this method.

field: the calendar field.

amount: the amount of date or time to be added to the field.

Returns: NA

Exceptions: NA

Approach

Java

import java.util.Calendar;

public class Calendaradd {
    public static void main(String[] args) {

        // create a calendar
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 2);

        System.out.println(calendar.getTime());
    }
}

Output:

Sat Jan 22 20:54:32 IST 2022


No comments:

Post a Comment