Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

How to Roll Through Hours & Months in Java



The Calendar class provides the roll() method to adjust the date or time without changing higher fields in Java. For example, you can roll through months or hours while keeping other parts of the date unchanged. Following are the different examples we will be implementing

  • Rolling through months and hours

  • Rolling through year and hour

Rolling through months and hours

Calendar class: A class that provides methods for manipulating date and time. It allows you to perform operations such as adding, subtracting, or rolling through dates and times.

roll() method: The roll() method in the Calendar class adjusts the specified field (e.g., month, hour) without affecting the other fields, such as year or day.

Steps

The steps for rolling through months and hours are as follows:

Step 1. Create a Date object: Initialize a Date object to store the current date and time.

Date d1 = new Date();

Step 2. Create a Calendar instance: Use the Calendar class to create an instance representing the current date.

Calendar cl = Calendar.getInstance();

Step 3. Set Calendar time to the Date object: Set the Calendar objects time to the Date object.

cl.setTime(d1);

Example

The following example shows us how to roll through months (without changing year) or hrs(without changing month or year) using the roll() method of class calendar

import java.util.*;

public class Main {
   public static void main(String[] args) throws Exception {
      Date d1 = new Date();
      Calendar cl = Calendar. getInstance();
      
      cl.setTime(d1);
      System.out.println("today is "+ d1.toString());
      
      cl. roll(Calendar.MONTH, 100);
      System.out.println("date after a month will be " + cl.getTime().toString() );
      
      cl. roll(Calendar.HOUR, 70);
      System.out.println("date after 7 hrs will be "+ cl.getTime().toString() );
   }
}

Output

today is Mon Jun 22 02:44:36 IST 2009
date after a month will be Thu Oct 22 02:44:36 IST 2009
date after 7 hrs will be Thu Oct 22 00:44:36 IST 2009

Rolling through year and hour

The Calendar instance cal is used to manipulate the current time. The roll(Calendar.YEAR, false) method rolls the year down by one, and the roll(Calendar.HOUR, true) method rolls the hour up by one, adjusting the time accordingly without changing the day, month, or other fields.

Example

The following is another example of Roll month

import java.util.Calendar;

public class CalendarExample {
   public static void main(String[] args) {
      Calendar cal = Calendar.getInstance();
		System.out.println("Time:" + cal.getTime());
		
      cal.roll(Calendar.YEAR, false);
		System.out.println("Time rolling down the year:" + cal.getTime());
		
      cal.roll(Calendar.HOUR, true);
		System.out.println("Time rolling up the hour:" + cal.getTime());
   }
}

Output

Time:Fri Nov 11 07:01:31 UTC 2016
Time rolling down the year:Wed Nov 11 07:01:31 UTC 2015
Time rolling up the hour:Wed Nov 11 08:01:31 UTC 2015
Advertisements