Class HinduCalendar
- All Implemented Interfaces:
Serializable,Comparable<HinduCalendar>,CalendarDate,ChronoDisplay,Temporal<CalendarDate>,VariantSource,LocalizedPatternSupport
The traditional Hindu calendar which exists in many regional variants throughout the Indian subcontinent.
This version actually supports all algorithmic variants including the old Hindu calendar.
These variants are described in the book "Calendrical Calculations" by Dershowitz/Reingold. Real
Hindu calendars published on websites can nevertheless deviate in detail. Users who wish to support modern
Hindu calendars can start with the enum HinduRule in order to construct a suitable variant. For example,
it is possible to set the default era for all calendar objects by setting
the desired era on the variant. Users can also configure astronomic calculations to be applied instead of
the traditional ways to calculate Hindu calendar dates.
Supported elements
Following elements which are declared as constants are registered by this class:
Furthermore, all elements defined in EpochDays and CommonElements.RELATED_GREGORIAN_YEAR
are supported.
Calendar arithmetic and time units
A date arithmetic using units beyond the class CalendarDays is not offered.
But there are methods like previousMonth() or nextYear(). About years, user can also use
expressions like with(YEAR_OF_ERA, getYear() + amount).
Formatting and parsing
This calendar can deploy the same localized resources like the Indian national calendar. Example:
ChronoFormatter<HinduCalendar> f =
ChronoFormatter.ofPattern(
"G, d. MMMM yyyy",
PatternType.CLDR,
Locale.ENGLISH,
HinduCalendar.family());
HinduCalendar cal = HinduCalendar.ofOldSolar(3101, HinduMonth.of(IndianMonth.MAGHA).getRasi(), 19);
assertThat(
f.print(cal),
is("K.Y, 19. Magha 3101"));
Possible leap months or leap days can be printed and parsed, too. If no special format attribute is specified and the text width to be used is wide then the localized word for "adhika" will be printed in front of leap months or days. Example for a short display using format attributes for handling leap indicators and the orientation of leap indicator:
ChronoFormatter<HinduCalendar> f =
ChronoFormatter.ofPattern("M yyyy, d", PatternType.CLDR, Locale.ENGLISH, HinduCalendar.family())
.with(HinduPrimitive.ADHIKA_INDICATOR, '*')
.with(HinduPrimitive.ADHIKA_IS_TRAILING, true);
HinduCalendar cal =
HinduCalendar.of(
HinduRule.AMANTA.variant(),
HinduEra.VIKRAMA,
1549,
HinduMonth.of(IndianMonth.VAISHAKHA).withLeap(),
HinduDay.valueOf(3));
assertThat(
f.print(cal),
is("2* 1549, 3"));
Other number systems can be used in the usual way by obtaining a modified copy of the formatter
with an expression like f.with(Attributes.NUMBER_SYSTEM, NumberSystem.DEVANAGARI).
Oddities
The Hindu calendar knows lost days and leap days. And the lunisolar variants also know lost months
(rare) and leap months. This is the main reason why days and months are not modelled as integers. So users cannot
rely on simple home grown integer arithmetic to look for example for the next valid day but must use the existing
element queries, element manipulations and expressions based on the available methods like nextDay() or
nextMonth(). Any date input in doubt should also be validated using
isValid(HinduVariant, HinduEra, int, HinduMonth, HinduDay) when creating a calendar date.
- Since:
- 5.7
- Author:
- Meno Hochschild
- See Also:
IndianCalendar,HinduVariant,HinduRule,AryaSiddhanta,NumberSystem,HinduPrimitive.ADHIKA_INDICATOR,HinduPrimitive.ADHIKA_IS_TRAILING, Serialized Form
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic AdjustableTextElement<HinduDay>Represents the Hindu day of month.static StdCalendarElement<Weekday,HinduCalendar>Represents the Hindu day of week.static StdCalendarElement<Integer,HinduCalendar>Represents the Hindu day of year.static ChronoElement<HinduEra>Represents the Hindu era.static AdjustableTextElement<HinduMonth>Represents the Hindu month.static StdCalendarElement<Integer,HinduCalendar>Represents the Hindu year. -
Method Summary
Modifier and TypeMethodDescriptionCreates a new local timestamp with this date and given civil time.atTime(int hour, int minute)Is equivalent toat(PlainTime.of(hour, minute)).booleanCompares the whole state of this instance with given object.static CalendarFamily<HinduCalendar>family()Returns the associated calendar family.Obtains the day of month.Determines the day of week.intObtains the day of year.longCounts the elapsed days since UTC epoch.getEra()Obtains the era from the current Hindu variant.getMonth()Obtains the month.Yields the variant name of a calendar system.intgetYear()Obtains the year according to the current era and according to if the current Hindu variant uses elapsed years or current years.inthashCode()Subclasses must redefine this method corresponding to the behaviour ofequals().static booleanisValid(HinduVariant variant, HinduEra era, int yearOfEra, HinduMonth month, HinduDay dayOfMonth)Queries if given parameter values form a well defined Hindu date before instantiating the date.intYields the length of current Hindu month in days.intYields the length of current Hindu year in days.nextDay()Obtains the next day.Obtains the corresponding date of next month.nextYear()Obtains the corresponding date of next year.static HinduCalendarnowInSystemTime(HinduVariant variant)Obtains the current calendar date in system time.static HinduCalendarnowInSystemTime(HinduVariant variant, StartOfDay startOfDay)Obtains the current calendar date in system time.static HinduCalendarof(HinduVariant variant, HinduEra era, int yearOfEra, HinduMonth month, HinduDay dayOfMonth)Creates an Hindu calendar with given components.static HinduCalendarofOldLunar(int year, HinduMonth month, int dayOfMonth)Creates an old lunisolar Hindu calendar with given components.static HinduCalendarofOldSolar(int year, int month, int dayOfMonth)Creates an old solar Hindu calendar with given components.Obtains the previous day.Obtains the corresponding date of previous month.Obtains the corresponding date of previous year.toString()Provides a complete textual representation of the state of this calendar variant.Methods inherited from class net.time4j.engine.CalendarVariant
compareTo, isAfter, isBefore, isSimultaneous, minus, plus, withVariant, withVariantMethods inherited from class net.time4j.engine.ChronoEntity
contains, get, get, getInt, getMaximum, getMinimum, getRegisteredElements, getTimezone, hasTimezone, isValid, isValid, isValid, matches, with, with, with, withMethods inherited from interface net.time4j.engine.CalendarDate
transform, transform, transform, transform, transform, transformMethods inherited from interface net.time4j.format.LocalizedPatternSupport
getFormatPattern, getFormatPattern, useDynamicFormatPatternMethods inherited from interface net.time4j.engine.Temporal
isAfterAll, isAfterOrEqual, isBeforeAll, isBeforeOrEqual
-
Field Details
-
ERA
Represents the Hindu era.
A change of the era by
with()will not change the Hindu date in a temporal way but only the year representation. The changed era is stored in the variant of this calendar object. Attention: The old Hindu calendar ignores any era change and only supports Kali Yuga. -
YEAR_OF_ERA
@FormattableElement(format="y") public static final StdCalendarElement<Integer,HinduCalendar> YEAR_OF_ERARepresents the Hindu year.
The range is for the era Kali Yuga defined by
0-5999(elapsed year) resprective1-6000(current year) and will be adjusted accordingly for other eras. However, the modern Hindu calendar will use the year1200as minimum elapsed year. -
MONTH_OF_YEAR
Represents the Hindu month.
The first lunar month should always be determined by
with(MONTH_OF_YEAR.minimized())because the first month in lunisolar context might be in leap state. -
DAY_OF_MONTH
Represents the Hindu day of month.
The first day of lunar month should always be determined by
with(DAY_OF_MONTH.minimized())because the number of the first day of month in lunisolar context is not always 1. Similar thoughts also for the last day of lunar month. -
DAY_OF_YEAR
@FormattableElement(format="D") public static final StdCalendarElement<Integer,HinduCalendar> DAY_OF_YEARRepresents the Hindu day of year.
New Year can be determined by
with(DAY_OF_YEAR, 1)orwith(DAY_OF_YEAR.minimized()). -
DAY_OF_WEEK
@FormattableElement(format="E") public static final StdCalendarElement<Weekday,HinduCalendar> DAY_OF_WEEKRepresents the Hindu day of week.
If the day-of-week is set to a new value then Time4J handles the Hindu calendar week as starting on Sunday.
-
-
Method Details
-
nowInSystemTime
Obtains the current calendar date in system time.
The time of sunrise in the holy city Ujjain is used as start of day.
- Parameters:
variant- calendar variant- Returns:
- current calendar date in system time zone using the system clock
- See Also:
nowInSystemTime(HinduVariant, StartOfDay)
-
nowInSystemTime
Obtains the current calendar date in system time.
Convenient short-cut for:
SystemClock.inLocalView().now(HinduCalendar.family(), variant, startOfDay).toDate()).- Parameters:
variant- calendar variantstartOfDay- determines the exact time of day when the calendar date will change (usually at sunrise)- Returns:
- current calendar date in system time zone using the system clock
- See Also:
SystemClock.inLocalView(),ZonalClock.now(CalendarFamily, VariantSource, StartOfDay)
-
ofOldSolar
Creates an old solar Hindu calendar with given components.
The months use rasi numbers and start with VAISAKHA.
- Parameters:
year- expired year of era Kali Yugamonth- month number (rasi numbering in range 1-12)dayOfMonth- the day of given month in range 1-31- Returns:
- HinduCalendar
- Throws:
IllegalArgumentException- in case of any inconsistencies- See Also:
HinduMonth.getRasi(),AryaSiddhanta
-
ofOldLunar
Creates an old lunisolar Hindu calendar with given components.
The month CHAITRA is the first month of year.
- Parameters:
year- expired year of era Kali Yugamonth- the Hindu month (possibly as leap month)dayOfMonth- the day of given month- Returns:
- HinduCalendar
- Throws:
IllegalArgumentException- in case of any inconsistencies- See Also:
AryaSiddhanta
-
of
public static HinduCalendar of(HinduVariant variant, HinduEra era, int yearOfEra, HinduMonth month, HinduDay dayOfMonth)Creates an Hindu calendar with given components.
Note: The modern variants of Hindu calendar use the year
1200as miminum elapsed year.- Parameters:
variant- the variant of Hindu calendarera- the desired erayearOfEra- the year of given era (expired or current according to variant configuration)month- the Hindu month (in lunisolar case possibly as leap month)dayOfMonth- the day of given month (in lunisolar case possibly in leap state)- Returns:
- HinduCalendar
- Throws:
IllegalArgumentException- in case of any inconsistencies
-
isValid
public static boolean isValid(HinduVariant variant, HinduEra era, int yearOfEra, HinduMonth month, HinduDay dayOfMonth)Queries if given parameter values form a well defined Hindu date before instantiating the date.
Example for a non-existing day:
assertThat( HinduCalendar.isValid( AryaSiddhanta.LUNAR.variant(), HinduEra.KALI_YUGA, 0, HinduMonth.of(IndianMonth.CHAITRA).withLeap(), HinduDay.valueOf(15)), // expunged day! is(false));- Parameters:
variant- the variant of Hindu calendarera- the desired erayearOfEra- the year of given era (expired or current according to variant configuration)month- the Hindu month (in lunisolar case possibly as leap month)dayOfMonth- the day of given month (in lunisolar case possibly in leap state)- Returns:
trueif valid elsefalse- See Also:
of(HinduVariant, HinduEra, int, HinduMonth, HinduDay),HinduRule.variant(),AryaSiddhanta.variant()
-
lengthOfMonth
public int lengthOfMonth()Yields the length of current Hindu month in days.
Attention: This method obtains the real length while an expression like
getMaximum(DAY_OF_MONTH).getValue()only shows the number of last day of month which can be different from the length of month - especially in a lunisolar context.- Returns:
- int
-
lengthOfYear
public int lengthOfYear()Yields the length of current Hindu year in days.
- Returns:
- int
-
getVariant
Description copied from interface:VariantSourceYields the variant name of a calendar system.
- Specified by:
getVariantin interfaceVariantSource- Returns:
- String which is empty if there are no variants
-
getEra
Obtains the era from the current Hindu variant.
If the associated (elapsed) year becomes negative then the method will fall back to Kali Yuga era.
- Returns:
- HinduEra
- See Also:
ERA,HinduVariant.getDefaultEra()
-
getYear
public int getYear()Obtains the year according to the current era and according to if the current Hindu variant uses elapsed years or current years.
- Returns:
- int
- See Also:
YEAR_OF_ERA,getEra(),HinduEra.yearOfEra(HinduEra, int),HinduVariant.isUsingElapsedYears()
-
getMonth
Obtains the month.
- Returns:
- HinduMonth
- See Also:
MONTH_OF_YEAR
-
getDayOfMonth
Obtains the day of month.
- Returns:
- HinduDay
- See Also:
DAY_OF_MONTH,withFirstDayOfMonth()
-
getDayOfWeek
Determines the day of week.
- Returns:
- Weekday
- See Also:
DAY_OF_WEEK
-
getDayOfYear
public int getDayOfYear()Obtains the day of year.
- Returns:
- int
- See Also:
DAY_OF_YEAR,withNewYear()
-
previousDay
Obtains the previous day.
- Returns:
- HinduCalendar
- Throws:
IllegalArgumentException- if the adjusted date is out of range
-
previousMonth
Obtains the corresponding date of previous month.
- Returns:
- HinduCalendar
- Throws:
IllegalArgumentException- if the adjusted date is out of range
-
previousYear
Obtains the corresponding date of previous year.
- Returns:
- HinduCalendar
- Throws:
IllegalArgumentException- if the adjusted date is out of range
-
nextDay
Obtains the next day.
- Returns:
- HinduCalendar
- Throws:
IllegalArgumentException- if the adjusted date is out of range
-
nextMonth
Obtains the corresponding date of next month.
- Returns:
- HinduCalendar
- Throws:
IllegalArgumentException- if the adjusted date is out of range
-
nextYear
Obtains the corresponding date of next year.
- Returns:
- HinduCalendar
- Throws:
IllegalArgumentException- if the adjusted date is out of range
-
at
Creates a new local timestamp with this date and given civil time.
If the time
T24:00is used then the resulting timestamp will automatically be normalized such that the timestamp will contain the following day instead.- Parameters:
time- wall time- Returns:
- general timestamp as composition of this date and given time
-
atTime
Is equivalent to
at(PlainTime.of(hour, minute)).- Parameters:
hour- hour of day in range (0-24)minute- minute of hour in range (0-59)- Returns:
- general timestamp as composition of this date and given time
- Throws:
IllegalArgumentException- if any argument is out of range- See Also:
at(PlainTime)
-
equals
Description copied from class:CalendarVariantCompares the whole state of this instance with given object.
Implementations will usually define their state based on the temporal position and the variant name. Exceptions from this rule should be explicitly documented and reasoned.
- Specified by:
equalsin classCalendarVariant<HinduCalendar>- See Also:
CalendarVariant.compareTo(CalendarVariant)
-
hashCode
public int hashCode()Description copied from class:CalendarVariantSubclasses must redefine this method corresponding to the behaviour of
equals().- Specified by:
hashCodein classCalendarVariant<HinduCalendar>
-
toString
Description copied from class:CalendarVariantProvides a complete textual representation of the state of this calendar variant.
- Specified by:
toStringin classCalendarVariant<HinduCalendar>
-
getDaysSinceEpochUTC
public long getDaysSinceEpochUTC()Description copied from interface:CalendarDateCounts the elapsed days since UTC epoch.
- Specified by:
getDaysSinceEpochUTCin interfaceCalendarDate- Overrides:
getDaysSinceEpochUTCin classCalendarVariant<HinduCalendar>- Returns:
- count of days relative to UTC epoch [1972-01-01]
- See Also:
EpochDays.UTC
-
family
Returns the associated calendar family.
- Returns:
- chronology as calendar family
-