public enum CalendarUnit extends Enum<CalendarUnit> implements IsoDateUnit
Represents the most common time units related to a standard ISO-8601-calendar.
Default behaviour of addition or subtraction of month-related units:
If the addition of months results in an invalid intermediate date then the final date will be just the last valid date that is the last day of current month. Example:
PlainDate date = PlainDate.of(2013, 1, 31); System.out.println(date.plus(1, MONTHS)); // Output: 2013-02-28
| Enum Constant and Description |
|---|
CENTURIES
Time unit "centuries" (symbol C)
|
DAYS
Time unit "days" (symbol D)
|
DECADES
Time unit "decades" (symbol E)
|
MILLENNIA
Time unit "millennia" (symbol I)
|
MONTHS
Time unit "months" (symbol M)
|
QUARTERS
Time unit "quarter years" (symbol Q)
|
WEEKS
Time unit "weeks" (symbol W)
|
YEARS
Time unit "calendar years" (symbol Y)
|
| Modifier and Type | Method and Description |
|---|---|
IsoDateUnit |
atEndOfMonth()
Defines a variation of this unit which always sets the resulting
date in additions and subtractions to the end of month even if there
is no day overflow.
|
<T extends TimePoint<? super CalendarUnit,T>> |
between(T start,
T end)
Calculates the temporal distance between given calendar dates
in this calendar unit.
|
boolean |
isCalendrical()
A calendar unit is always calendrical.
|
IsoDateUnit |
keepingEndOfMonth()
Defines a variation of this unit which sets the resulting date
in additions and subtractions to the end of month if and only if the original
date is the last day of month.
|
IsoDateUnit |
nextValidDate()
Defines a variation of this unit which resolves invalid intermediate
dates in additions and subtractions to the first valid date after
(the first day of following month).
|
IsoDateUnit |
unlessInvalid()
Defines a variation of this unit which handles invalid
intermediate dates in additions and subtractions by throwing
a
ChronoException. |
static CalendarUnit |
valueOf(String name)
Returns the enum constant of this type with the specified name.
|
static CalendarUnit[] |
values()
Returns an array containing the constants of this enum type, in
the order they are declared.
|
static IsoDateUnit |
weekBasedYears()
Defines a special calendar unit for week-based years which are
not bound to the calendar year but to the week cycle of a year
preserving the day of week and (if possible) the week of year.
|
IsoDateUnit |
withCarryOver()
Defines a variation of this unit which resolves invalid intermediate
dates in additions and subtractions by transferring any day overflow
to the following month.
|
IsoDateUnit |
withJodaMetric()
Defines a variation of this unit which simulates the behaviour of Joda-Time.
|
compareTo, equals, getDeclaringClass, hashCode, name, ordinal, toString, valueOfgetLengthpublic static final CalendarUnit MILLENNIA
public static final CalendarUnit CENTURIES
public static final CalendarUnit DECADES
public static final CalendarUnit YEARS
public static final CalendarUnit QUARTERS
public static final CalendarUnit MONTHS
public static final CalendarUnit WEEKS
public static final CalendarUnit DAYS
public static CalendarUnit[] values()
for (CalendarUnit c : CalendarUnit.values()) System.out.println(c);
public static CalendarUnit valueOf(String name)
name - the name of the enum constant to be returned.IllegalArgumentException - if this enum type has no constant with the specified nameNullPointerException - if the argument is nullpublic <T extends TimePoint<? super CalendarUnit,T>> long between(T start, T end)
Calculates the temporal distance between given calendar dates in this calendar unit.
T - generic type of calendar datestart - starting dateend - ending datepublic boolean isCalendrical()
A calendar unit is always calendrical.
isCalendrical in interface ChronoUnittruepublic IsoDateUnit nextValidDate()
Defines a variation of this unit which resolves invalid intermediate dates in additions and subtractions to the first valid date after (the first day of following month).
Example for months:
PlainDate date = PlainDate.of(2013, 1, 31); System.out.println(date.plus(1, MONTHS.nextValidDate())); // Output: 2013-03-01
Note: The metric for calculation of temporal distances remains unaffected.
public IsoDateUnit withCarryOver()
Defines a variation of this unit which resolves invalid intermediate dates in additions and subtractions by transferring any day overflow to the following month.
Example for months:
PlainDate date = PlainDate.of(2013, 1, 31); System.out.println(date.plus(1, MONTHS.withCarryOver())); // Output: 2013-03-03
Note: The metric for calculation of temporal distances remains unaffected.
public IsoDateUnit unlessInvalid()
Defines a variation of this unit which handles invalid
intermediate dates in additions and subtractions by throwing
a ChronoException.
Example for months:
PlainDate date = PlainDate.of(2013, 1, 31); System.out.println(date.plus(1, MONTHS.unlessInvalid())); // February 31th does not exist => throws ChronoException
Note: The metric for calculation of temporal distances remains unaffected.
public IsoDateUnit atEndOfMonth()
Defines a variation of this unit which always sets the resulting date in additions and subtractions to the end of month even if there is no day overflow.
Example for months:
PlainDate date1 = PlainDate.of(2013, 2, 27); System.out.println(date1.plus(2, MONTHS.atEndOfMonth())); // Ausgabe: 2013-04-30 PlainDate date2 = PlainDate.of(2013, 2, 28); System.out.println(date2.plus(2, MONTHS.atEndOfMonth())); // Ausgabe: 2013-04-30
Note: The metric for calculation of temporal distances has been changed since v3.35/4.30: The day-of-month-criterion will no longer be directly applied but the intermediate result will be adjusted after having done an addition step.
An alternative which only jumps to the end of month
if the original date is the last day of month can be achieved
by keepingEndOfMonth().
UnsupportedOperationException - if this unit is day- or week-relatedpublic IsoDateUnit keepingEndOfMonth()
Defines a variation of this unit which sets the resulting date in additions and subtractions to the end of month if and only if the original date is the last day of month.
Example for months:
PlainDate date1 = PlainDate.of(2013, 2, 27); System.out.println(date1.plus(2, MONTHS.keepingEndOfMonth())); // Ausgabe: 2013-04-27 PlainDate date2 = PlainDate.of(2013, 2, 28); System.out.println(date2.plus(2, MONTHS.keepingEndOfMonth())); // Ausgabe: 2013-04-30
Note: The metric for calculation of temporal distances has been changed since v3.35/4.30: The day-of-month-criterion will no longer be directly applied but the intermediate result will be adjusted after having done an addition step.
An alternative which unconditionally jumps to the end
of month can be achieved by atEndOfMonth().
UnsupportedOperationException - if this unit is day- or week-relatedpublic IsoDateUnit withJodaMetric()
Defines a variation of this unit which simulates the behaviour of Joda-Time.
Example for years:
PlainDate birthDate = PlainDate.of(1996, 2, 29);
PlainDate currentDate = PlainDate.of(2014, 2, 28);
IsoDateUnit jodaUnit = YEARS.withJodaMetric();
Duration<IsoDateUnit> d = Duration.in(jodaUnit).between(birthDate, currentDate);
System.out.println(d); // Output: P18{Y-JODA_METRIC}
assertThat(d.getPartialAmount(jodaUnit), is(18L));
assertThat(birthDate.plus(18, jodaUnit), is(currentDate));
assertThat(birthDate.until(currentDate, jodaUnit), is(18L)); // Joda-metric
assertThat(birthDate.until(currentDate, CalendarUnit.YEARS), is(17L)); // standard metric
Note: Users should not use this unit for age calculations.
public static IsoDateUnit weekBasedYears()
Defines a special calendar unit for week-based years which are not bound to the calendar year but to the week cycle of a year preserving the day of week and (if possible) the week of year.
Note: If the week of year is originally 53, but there is no such value after addition or subtraction then the week of year will be reduced to value 52.
PlainDate start = PlainDate.of(2000, 2, 29); // 2000-W09-2 System.out.println(start.plus(14, CalendarUnit.weekBasedYears())); // Output: 2014-02-25 (= 2014-W09-2)
Weekcycle.YEARSCopyright © 2014–2017. All rights reserved.