Question

So let's say I have this code:

//someParameterizedDate = New Years Eve 2011
Calendar cal = new GregorianCalendar();
cal.setTime(someParameterizedDate);
cal.roll(Calendar.DAY_OF_YEAR, 1);

Will the calendar now be equal to january 1, 2012? I found all the JavaDocs a little confusing.

Was it helpful?

Solution

java.util.Calendar

roll(f, delta) adds delta to field f without changing larger fields. This is equivalent to calling add(f, delta) with the following adjustment:

Roll rule. Larger fields are unchanged after the call. A larger field represents a larger unit of time. DAY_OF_MONTH is a larger field than HOUR.

You roll with DAY_OF_YEAR which means it will not affect MONTH or YEAR which are larger units

So basically, you should get you to December 1st 2011

You can use add if you want it to go to January 1st 2012

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top