Enum Class CalendarUnit
- All Implemented Interfaces:
Serializable
,Comparable<CalendarUnit>
,java.lang.constant.Constable
,ChronoUnit
,IsoDateUnit
,IsoUnit
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
- Author:
- Meno Hochschild
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>>
-
Enum Constant Summary
Enum ConstantDescriptionTime unit "centuries" (symbol C)Time unit "days" (symbol D)Time unit "decades" (symbol E)Time unit "millennia" (symbol I)Time unit "months" (symbol M)Time unit "quarter years" (symbol Q)Time unit "weeks" (symbol W)Time unit "calendar years" (symbol Y) -
Method Summary
Modifier and TypeMethodDescriptionDefines 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>>
longbetween(T start, T end)
Calculates the temporal distance between given calendar dates in this calendar unit.boolean
A calendar unit is always calendrical.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.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).Defines a variation of this unit which handles invalid intermediate dates in additions and subtractions by throwing aChronoException
.static CalendarUnit
Returns the enum constant of this class with the specified name.static CalendarUnit[]
values()
Returns an array containing the constants of this enum class, in the order they are declared.static IsoDateUnit
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.Defines a variation of this unit which resolves invalid intermediate dates in additions and subtractions by transferring any day overflow to the following month.Defines a variation of this unit which simulates the behaviour of Joda-Time.Methods inherited from class java.lang.Enum
compareTo, describeConstable, equals, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
Methods inherited from interface net.time4j.engine.ChronoUnit
getLength
-
Enum Constant Details
-
MILLENNIA
Time unit "millennia" (symbol I) -
CENTURIES
Time unit "centuries" (symbol C) -
DECADES
Time unit "decades" (symbol E) -
YEARS
Time unit "calendar years" (symbol Y) -
QUARTERS
Time unit "quarter years" (symbol Q) -
MONTHS
Time unit "months" (symbol M) -
WEEKS
Time unit "weeks" (symbol W) -
DAYS
Time unit "days" (symbol D)
-
-
Method Details
-
values
Returns an array containing the constants of this enum class, in the order they are declared.- Returns:
- an array containing the constants of this enum class, in the order they are declared
-
valueOf
Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum class has no constant with the specified nameNullPointerException
- if the argument is null
-
between
Calculates the temporal distance between given calendar dates in this calendar unit.
- Type Parameters:
T
- generic type of calendar date- Parameters:
start
- starting dateend
- ending date- Returns:
- duration as count of this unit
-
isCalendrical
public boolean isCalendrical()A calendar unit is always calendrical.
- Specified by:
isCalendrical
in interfaceChronoUnit
- Returns:
true
-
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.
- Returns:
- calendar unit with modified addition behaviour if month-based, but still the same metric
-
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.
- Returns:
- calendar unit with modified addition behaviour if month-based, but still the same metric
-
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.
- Returns:
- calendar unit with modified addition behaviour if month-based, but still the same metric
-
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()
.- Returns:
- calendar unit with modified addition behaviour and modified metric (if month-based)
- Throws:
UnsupportedOperationException
- if this unit is day- or week-related
-
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()
.- Returns:
- calendar unit with modified addition behaviour and modified metric if month-based
- Throws:
UnsupportedOperationException
- if this unit is day- or week-related- Since:
- 2.3
-
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.
- Returns:
- calendar unit with default addition behaviour but modified metric if month-based
- Since:
- 3.35/4.30
-
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)
- Returns:
- calendar unit for week-based years
- See Also:
Weekcycle.YEARS
-