Calendar roll() Method in Java with Examples
The roll(int calndr_field, boolean up_down) method in Calendar class is used to operate on the passed calendar field by moving the passed field, up or down by a single unit of time. This involves addition or subtraction of the time field without changing the larger fields.
public abstract void roll(int calndr_field, boolean up_down)
Parameters: The method takes two parameters:
- calndr_field: This is of Calendar type and refers to the field of the calendar that is to be operated upon.
- up_down: This is of the boolean type that is used to indicate whether to move calndr_field up or down or to increase or decrease. The true indicates adding a unit of time and false subtracts the same.
Return Value: The method does not return any value.
Below programs illustrate the working of roll() Method of Calendar class:
The Current Year is: 2019 The New Year is: 2018 The new year is: 2019
The Current Month is: 1 The New Month is: 2 The new month is: 1