Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calendar add() vs roll() when do we use it?

Tags:

java

calendar

I know add() adds the specified (signed) amount of time to the given time field, based on the calendar's rules.

And roll() adds the specified (signed) single unit of time on the given time field without changing larger fields.

I can't think of an everyday usage of roll() I would do everything by add().

Can you help me out with examples when do we use roll() and when add()?

EDIT 1

Joda answers are not accepted!

like image 288
Pentium10 Avatar asked Mar 23 '10 22:03

Pentium10


People also ask

What roll Calendar?

Calendar RollsThe calendering process of smoothing and compressing a sheet of material by passing it through a number of pairs of heated rolls is widely used in the manufacture of paper, textiles, and plastic sheeting to provide it with the desired surface finish and texture.

What does Calendar getInstance() return?

Calendar 's getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time: Calendar rightNow = Calendar.

What is roll Java?

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.

How does Calendar work Java?

Calendar class in Java is an abstract class that provides methods for converting date between a specific instant in time and a set of calendar fields such as MONTH, YEAR, HOUR, etc. It inherits Object class and implements the Comparable, Serializable, Cloneable interfaces.


3 Answers

  • add() - almost always, as you said
  • roll() - for example you want to "dispense" events in one month. The algorithm may be to proceed a number of days and place the event, then proceed further. When the end of the month is reached, it should start over from the beginning. Hence roll().
like image 130
Bozho Avatar answered Oct 31 '22 07:10

Bozho


Found in jGuru

  • Calendar.roll()
    Changes a specific unit and leaves 'larger' (in terms of time-month is 'larger' than day) units unchanged. The API example is that given a date of August 31, 1999, rolling by (Calendar.MONTH, 8) yields April 30, 1999. That is, the DAY was changed to meet April's maximum, but the 'larger' unit, YEAR, was unchanged.

roll(): Rolls up 8 months here i.e., adding 8 months to Aug will result in Apr but year remains unchanged(untouched).

  • Calendar.add()
    Will cause the next 'larger' unit to change, if necessary. That is, given a date of August 31, 1999, add(Calendar.MONTH, 8) yields April 30, 2000. add() also forces a recalculation of milliseconds and all fields.

add(): Adds months to the current date i.e., adding 8 months to Aug will give Apr of Next Year, hence forces the Year change.

like image 32
Menda Avatar answered Oct 31 '22 07:10

Menda


I was just asking the same question (which is how I found this page) and someone at my work place (well done, DCK) came up with a suggestion:

The date selectors on many smart phones (and other similar interfaces) will "roll" the day from the 31st to the 1st without altering the month, similarly for the month field.

I can't think of another use ATM and this one could be implemented in other ways, but at least it's an example!

Tim

like image 6
Tim Avatar answered Oct 31 '22 07:10

Tim