Calendar.Add(CalendarField, Int32) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Add a signed amount to a specified field, using this calendar's rules.
[Android.Runtime.Register("add", "(II)V", "GetAdd_IIHandler", ApiSince=24)]
public virtual void Add (Android.Icu.Util.CalendarField field, int amount);
[<Android.Runtime.Register("add", "(II)V", "GetAdd_IIHandler", ApiSince=24)>]
abstract member Add : Android.Icu.Util.CalendarField * int -> unit
override this.Add : Android.Icu.Util.CalendarField * int -> unit
Parameters
- field
- CalendarField
the time field.
- amount
- Int32
the amount to add to the field.
- Attributes
Remarks
Add a signed amount to a specified field, using this calendar's rules. For example, to add three days to the current date, you can call add(Calendar.DATE, 3)
.
When adding to certain fields, the values of other fields may conflict and need to be changed. For example, when adding one to the #MONTH MONTH
field for the Gregorian date 1/31/96, the #DAY_OF_MONTH DAY_OF_MONTH
field must be adjusted so that the result is 2/29/96 rather than the invalid 2/31/96.
Adding a positive value always means moving forward in time, so for the Gregorian calendar, starting with 100 BC and adding +1 to year results in 99 BC (even though this actually reduces the numeric value of the field itself).
<strong>[icu] Note:</strong> The ICU implementation of this method is able to add to all fields except for #ERA ERA
, #DST_OFFSET DST_OFFSET
, and #ZONE_OFFSET ZONE_OFFSET
. Subclasses may, of course, add support for additional fields in their overrides of add
.
<b>Note:</b> You should always use roll
and add
rather than attempting to perform arithmetic operations directly on the fields of a Calendar
. It is quite possible for Calendar
subclasses to have fields with non-linear behavior, for example missing months or days during non-leap years. The subclasses' add
and roll
methods will take this into account, while simple arithmetic manipulations may give invalid results.
<b>Subclassing:</b><br> This implementation of add
assumes that the behavior of the field is continuous between its minimum and maximum, which are found by calling #getActualMinimum getActualMinimum
and #getActualMaximum getActualMaximum
. For such fields, simple arithmetic operations are sufficient to perform the add.
Subclasses that have fields for which this assumption of continuity breaks down must override add
to handle those fields specially. For example, in the Hebrew calendar the month "Adar I" only occurs in leap years; in other years the calendar jumps from Shevat (month #4) to Adar (month #6). The HebrewCalendar#add HebrewCalendar.add
method takes this into account, so that adding one month to a date in Shevat gives the proper result (Adar) in a non-leap year.
Java documentation for android.icu.util.Calendar.add(int, int)
.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.