Open In App

Java Program to Show Time By Rolling Through Hours and Months

Last Updated : 29 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Java.util package has a Calendar class which has some predefined methods, which helps us to do some operations related to date and time. Calendar class has a predefined method called roll() method, with the help of which we can roll through months (without changing the year) and hours(without changing the month and year).

Rolling basically means that getting back over the same state after a certain period. For eg, let’s consider that it is the month of October, and we have rolled over the month by 50, it means that we have rolled by 2 months, ie (50%12=2), since there is a total of 12 months in a year, so current month after rolling by 50 months would be December.

Consider another example that let’s say we have rolled over hours by the amount 76 and current status of time is 5:00 pm and date is 22 October 2020, so after we have rolled for 76 hours, the status of time will be 9:00 pm and data will be 22 October 2020, it is so because there is a total of 24 hours in a day and (76%24=4), so the current time status will only be increased by 4. 

Syntax:

public abstract void roll(int calendar_field, boolean up_down)

Here, roll() method takes 2 parameters calendar_field and up_down.

  • calendar_field: It is the hour or month or year on which we want to roll
  • up_down: It is the amount by which we have to roll, which is either a boolean value or an integer value. In the above examples, it is shown that how to roll for an integer value (it can be either both positive or a negative value), but for a boolean value, if the value is passed is true, it means that we have to roll by +1 amount and if the value passed is false, it means that roll by -1 amount.

Example: The below example explains how to use the roll() method programmatically.

Java




// Java program to show how to roll through
// hours or years or months using roll() method
import java.util.Calendar;
import java.util.Date;
 
public class CalendarExample1 {
    public static void main(String[] args) throws Exception
    {
        // getting the current date
        Date d1 = new Date();
       
          // create two calendar instances
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
       
        // setting the current date and time in calendar
        c1.setTime(d1);
        c2.setTime(d1);
           
          // print the current date
        System.out.println("Today is " + d1.toString());
 
        // rolling over month by 50
        c1.roll(Calendar.MONTH, 50);
        System.out.println(
            "Date after rolling by 50 over  month will be "
            + c1.getTime().toString());
 
        // rolling over hour by 70
        c1.roll(Calendar.HOUR, 70);
        System.out.println(
            "Date after rolling by 70 over hours will be "
            + c1.getTime().toString());
 
        // rolling over year by 2
        c1.roll(Calendar.YEAR, 2);
        System.out.println(
            "Date after  rolling by 2 over year is "
            + c1.getTime().toString());
 
        // false rolling over month
        c2.roll(Calendar.MONTH, false);
        System.out.println(
            "Date after false rolling over month will be "
            + c2.getTime().toString());
 
        // true rolling over hour
        c2.roll(Calendar.HOUR, true);
        System.out.println(
            "Date after true rolling over hour will be "
            + c2.getTime().toString());
 
        // true rolling over year
        c2.roll(Calendar.YEAR, true);
        System.out.println(
            "Date after true rolling over year is "
            + c2.getTime().toString());
    }
}


Output

Today is Fri Oct 30 06:19:15 UTC 2020
Date after rolling by 50 over  month will be Wed Dec 30 06:19:15 UTC 2020
Date after rolling by 70 over hours will be Wed Dec 30 04:19:15 UTC 2020
Date after  rolling by 2 over year is Fri Dec 30 04:19:15 UTC 2022
Date after false rolling over month will be Wed Sep 30 06:19:15 UTC 2020
Date after true rolling over hour will be Wed Sep 30 07:19:15 UTC 2020
Date after true rolling over year is Thu Sep 30 07:19:15 UTC 2021

Example 2: 

Java




// Java code to illustrate
// roll() method
 
import java.util.*;
 
public class CalendarExample2 {
    public static void main(String args[])
    {
        // Creating a calendar
        Calendar calndr = Calendar.getInstance();
 
        // Displaying the month
        System.out.println("The Current Month"
                           + " is: "
                           + calndr.get(Calendar.MONTH));
 
        // Incrementing the month
        // true indicates addition
        calndr.roll(Calendar.MONTH, true);
 
        // Displaying the result after operation
        System.out.println("The New Month is: "
                           + calndr.get(Calendar.MONTH));
 
        // Decrementing the month
        // false indicates subtraction
        calndr.roll(Calendar.MONTH, false);
 
        // Displaying the result after operation
        System.out.println("The new month is: "
                           + calndr.get(Calendar.MONTH));
    }
}


Output

The Current Month is: 9
The New Month is: 10
The new month is: 9


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads