Class PersianDate
- All Implemented Interfaces:
java.lang.Comparable<java.time.chrono.ChronoLocalDate>,java.time.chrono.ChronoLocalDate,java.time.temporal.Temporal,java.time.temporal.TemporalAccessor,java.time.temporal.TemporalAdjuster
@Immutable
public final class PersianDate
extends java.lang.Object
implements java.time.chrono.ChronoLocalDate
PersianDate is an immutable date-time object that represents a date,
often viewed as year-month-day.
In order to simplify usage of this class, it is tried to make API of this class
the same as JDK8 LocalDate class. Since some methods of LocalDate were
useful for Persian calendar system, they have been exactly copied. Some other methods of
HijrahDate and JapaneseDate have been
modified and used in this class.
This class is immutable and can be used in multi-threaded programs.
- Author:
- Mahmoud Fathi
-
Field Summary
Fields Modifier and Type Field Description static PersianDateMAXThe maximum supported persian date1999-12-29.static PersianDateMINThe minimum supported persian date0001-01-01. -
Method Summary
Modifier and Type Method Description booleanequals(java.lang.Object obj)Checks if this date is equal to another date.static PersianDatefrom(java.time.temporal.TemporalAccessor temporal)Obtains an instance ofPersianDatefrom a temporal object.static PersianDatefromGregorian(java.time.LocalDate localDate)Returns an instance ofPersianDatethat is correspondent to the gregorian date of parameterlocalDate.java.time.chrono.ChronologygetChronology()Gets the chronology of this date, which is the Persian calendar system.intgetDayOfMonth()java.time.DayOfWeekgetDayOfWeek()Returns day-of-week as an enumDayOfWeek.intgetDayOfYear()longgetLong(java.time.temporal.TemporalField field)Gets the value of the specified field from this date as along.PersianMonthgetMonth()intgetMonthValue()intgetYear()inthashCode()A hash code for this persian date.booleanisLeapYear()Returns true ifyearis a leap year in Persian calendar.static booleanisLeapYear(int year)Returnstrueif the given year is a leap year in Persian calendar.intlengthOfMonth()Returns the length of the month represented by this date.static PersianDatenow()Obtains current Persian date from the system clock in the default time zone.static PersianDateof(int year, int month, int dayOfMonth)Obtains an instance ofPersianDatewith year, month and day of month.static PersianDateof(int year, PersianMonth month, int dayOfMonth)Obtains an instance ofPersianDatewith year, month and day of month.static PersianDateofEpochDay(long epochDays)Returns an instance ofPersianDate, based on number of epoch days, which is from 1970-01-01.static PersianDateofJulianDays(long julianDays)Returns an instance ofPersianDate, based on number of julian days.static PersianDateparse(java.lang.CharSequence text)Obtains an instance ofPersianDatefrom a text, assuming its format isyyyy-MM-dd.static PersianDateparse(java.lang.CharSequence text, java.time.format.DateTimeFormatter formatter)Obtains an instance ofPersianDatefrom a text string using a specific formatter.PersianDateplusDays(long daysToAdd)Returns a copy of thisPersianDatewith the specified number of days added.PersianDateplusMonths(long monthsToAdd)Returns a copy of thisPersianDatewith the specified period in months added.PersianDateplusYears(long yearsToAdd)Returns a copy of thisPersianDatewith the specified period in years added.longtoEpochDay()java.time.LocalDatetoGregorian()Returns an equivalent Gregorian date and time as an instance ofLocalDate.longtoJulianDay()Returns number of corresponding julian days.java.lang.StringtoString()Returns the string representation of this persian date.java.time.chrono.ChronoPerioduntil(java.time.chrono.ChronoLocalDate endDateExclusive)Calculates the period between this date and another date as aPeriod.longuntil(java.time.temporal.Temporal endExclusive, java.time.temporal.TemporalUnit unit)Calculates the amount of time until another date in terms of the specified unit.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.time.chrono.ChronoLocalDate
adjustInto, atTime, compareTo, format, getEra, isAfter, isBefore, isEqual, isSupported, isSupported, lengthOfYear, minus, minus, plus, plus, query, with, withMethods inherited from interface java.time.temporal.TemporalAccessor
get, range
-
Field Details
-
MIN
The minimum supported persian date0001-01-01. -
MAX
The maximum supported persian date1999-12-29.
-
-
Method Details
-
getYear
public int getYear()- Returns:
- the year
-
getMonth
- Returns:
- the month-of-year field using the
Monthenum. - See Also:
getMonthValue()
-
getMonthValue
public int getMonthValue()- Returns:
- the month-of-year, from 1 to 12
- See Also:
getMonth()
-
getDayOfMonth
public int getDayOfMonth()- Returns:
- day-of-month, from 1 to 31
-
getDayOfYear
public int getDayOfYear()- Returns:
- day-of-year, from 1 to 365 or 366 in a leap year
-
getDayOfWeek
public java.time.DayOfWeek getDayOfWeek()Returns day-of-week as an enumDayOfWeek. This avoids confusion as to whatintmeans. If you need access to the primitiveintvalue then the enum provides theint value.- Returns:
- day-of-week, which is an enum
DayOfWeek
-
now
Obtains current Persian date from the system clock in the default time zone.- Returns:
- current Persian date from the system clock in the default time zone
-
of
Obtains an instance ofPersianDatewith year, month and day of month. The value of month must be between1and12. Value1would bePersianMonth.FARVARDINand value12representsPersianMonth.ESFAND.- Parameters:
year- the year to represent, from 1 to MAX_YEARmonth- the value of month, from 1 to 12dayOfMonth- the dayOfMonth to represent, from 1 to 31- Returns:
- an instance of
PersianDate - Throws:
java.time.DateTimeException- if the passed parameters do not form a valid date or time.
-
of
Obtains an instance ofPersianDatewith year, month and day of month.- Parameters:
year- the year to represent, from 1 to MAX_YEARmonth- the month-of-year to represent, an instance ofPersianMonthdayOfMonth- the dayOfMonth to represent, from 1 to 31- Returns:
- an instance of
PersianDate - Throws:
java.time.DateTimeException- if the passed parameters do not form a valid date or time.
-
fromGregorian
Returns an instance ofPersianDatethat is correspondent to the gregorian date of parameterlocalDate.- Parameters:
localDate- Gregorian date and time, not null- Returns:
- an equivalent Persian date and time as an instance of
PersianDate
-
parse
Obtains an instance ofPersianDatefrom a text, assuming its format isyyyy-MM-dd. For example the given text could be1399-10-12, otherwise an exception will be thrown.- Parameters:
text- the text to parse, notnull- Returns:
- an instance of
PersianDatefrom the given text
-
parse
public static PersianDate parse(java.lang.CharSequence text, java.time.format.DateTimeFormatter formatter)Obtains an instance ofPersianDatefrom a text string using a specific formatter.- Parameters:
text- the text to parse, notnullformatter- the formatter to use, notnull- Returns:
- an instance of
PersianDatefrom the given text
-
from
Obtains an instance ofPersianDatefrom a temporal object.- Parameters:
temporal- the temporal object to convert, notnull- Returns:
- the local date, not
null - Throws:
java.time.DateTimeException- if unable to convert to aPersianDate
-
ofEpochDay
Returns an instance ofPersianDate, based on number of epoch days, which is from 1970-01-01. For example passing17468as the parameter results a persian date of 1396-08-07.- Parameters:
epochDays- epoch days- Returns:
- an instance of
PersianDate
-
ofJulianDays
Returns an instance ofPersianDate, based on number of julian days. For example passing2458055as the parameter will cause to get a Persian date of "1396-8-6".- Parameters:
julianDays- julian days- Returns:
- an instance of
PersianDate - See Also:
- libcalendars
-
getChronology
public java.time.chrono.Chronology getChronology()Gets the chronology of this date, which is the Persian calendar system.The
Chronologyrepresents the calendar system in use. The era and other fields inChronoFieldare defined by the chronology.- Specified by:
getChronologyin interfacejava.time.chrono.ChronoLocalDate- Returns:
- the Persian chronology, not null
-
lengthOfMonth
public int lengthOfMonth()Returns the length of the month represented by this date.This returns the length of the month in days.
- Specified by:
lengthOfMonthin interfacejava.time.chrono.ChronoLocalDate- Returns:
- the length of the month in days
-
until
public long until(java.time.temporal.Temporal endExclusive, java.time.temporal.TemporalUnit unit)Calculates the amount of time until another date in terms of the specified unit.This calculates the amount of time between two
PersianDateobjects in terms of a singleTemporalUnit. The start and end points arethisand the specified date. The result will be negative if the end is before the start. TheTemporalpassed to this method is converted to aPersianDateusingfrom(TemporalAccessor). For example, the amount in days between two dates can be calculated usingstartDate.until(endDate, DAYS).The calculation returns a whole number, representing the number of complete units between the two dates. For example, the amount in months between 1396-06-15 and 1396-08-14 will only be one month as it is one day short of two months.
There are two equivalent ways of using this method. The first is to invoke this method. The second is to use
TemporalUnit.between(Temporal, Temporal):// these two lines are equivalent amount = start.until(end, MONTHS); amount = MONTHS.between(start, end);
The choice should be made based on which makes the code more readable.The calculation is implemented in this method for
ChronoUnit. The unitsDAYS,WEEKS,MONTHS,YEARS,DECADES,CENTURIES,MILLENNIAandERASare supported. OtherChronoUnitvalues will throw an exception.If the unit is not a
ChronoUnit, then the result of this method is obtained by invokingTemporalUnit.between(Temporal, Temporal)passingthisas the first argument and the converted input temporal as the second argument.This instance is immutable and unaffected by this method call.
- Specified by:
untilin interfacejava.time.chrono.ChronoLocalDate- Specified by:
untilin interfacejava.time.temporal.Temporal- Parameters:
endExclusive- the end date, exclusive, which is converted to aPersianDate, not nullunit- the unit to measure the amount in, not null- Returns:
- the amount of time between this date and the end date
- Throws:
java.time.DateTimeException- if the amount cannot be calculated, or the end temporal cannot be converted to aPersianDatejava.time.temporal.UnsupportedTemporalTypeException- if the unit is not supportedjava.lang.ArithmeticException- if numeric overflow occurs
-
until
public java.time.chrono.ChronoPeriod until(java.time.chrono.ChronoLocalDate endDateExclusive)Calculates the period between this date and another date as aPeriod.This calculates the period between two dates in terms of years, months and days. The start and end points are
thisand the specified date. The result will be negative if the end is before the start. The negative sign will be the same in each of year, month and day.The calculation is performed using the ISO calendar system. If necessary, the input date will be converted to ISO.
The start date is included, but the end date is not. The period is calculated by removing complete months, then calculating the remaining number of days, adjusting to ensure that both have the same sign. The number of months is then normalized into years and months based on a 12 month year. A month is considered to be complete if the end day-of-month is greater than or equal to the start day-of-month. For example, from
2010-01-15to2011-03-18is "1 year, 2 months and 3 days".There are two equivalent ways of using this method. The first is to invoke this method. The second is to use
Period.between(LocalDate, LocalDate):// these two lines are equivalent period = start.until(end); period = Period.between(start, end);
The choice should be made based on which makes the code more readable.- Specified by:
untilin interfacejava.time.chrono.ChronoLocalDate- Parameters:
endDateExclusive- the end date, exclusive, which may be in any chronology, not null- Returns:
- the period between this date and the end date, not null
-
getLong
public long getLong(java.time.temporal.TemporalField field)Gets the value of the specified field from this date as along.This queries this date for the value for the specified field. If it is not possible to return the value, because the field is not supported or for some other reason, an exception is thrown.
If the field is a
ChronoFieldthen the query is implemented here. Thesupported fieldswill return valid values based on this date. All otherChronoFieldinstances will throw anUnsupportedTemporalTypeException.If the field is not a
ChronoField, then the result of this method is obtained by invokingTemporalField.getFrom(TemporalAccessor)passingthisas the argument. Whether the value can be obtained, and what the value represents, is determined by the field.- Specified by:
getLongin interfacejava.time.temporal.TemporalAccessor- Parameters:
field- the field to get, not null- Returns:
- the value for the field
- Throws:
java.time.DateTimeException- if a value for the field cannot be obtainedjava.time.temporal.UnsupportedTemporalTypeException- if the field is not supportedjava.lang.ArithmeticException- if numeric overflow occurs
-
plusYears
Returns a copy of thisPersianDatewith the specified period in years added.This method adds the specified amount to the years field in three steps:
- Add the input years to the year field
- Check if the resulting date would be invalid
- Adjust the day-of-month to the last valid day if necessary
For example, 1387-12-30 (leap year) plus one year would result in the invalid date 1388-12-30 (standard year). Instead of returning an invalid result, the last valid day of the month, 1388-12-29, is selected instead.
This instance is immutable and unaffected by this method call.
- Parameters:
yearsToAdd- the years to add, may be negative- Returns:
- a
PersianDatebased on this date with the years added, not null - Throws:
java.time.DateTimeException- if the result exceeds the supported date range
-
plusMonths
Returns a copy of thisPersianDatewith the specified period in months added.This method adds the specified amount to the months field in three steps:
- Add the input months to the month-of-year field
- Check if the resulting date would be invalid
- Adjust the day-of-month to the last valid day if necessary
For example, 1388-11-30 plus one month would result in the invalid date 1388-12-30. Instead of returning an invalid result, the last valid day of the month, 1388-12-29, is selected instead.
This instance is immutable and unaffected by this method call.
- Parameters:
monthsToAdd- the months to add, may be negative- Returns:
- a
PersianDatebased on this date with the months added, not null - Throws:
java.time.DateTimeException- if the result exceeds the supported date range
-
plusDays
Returns a copy of thisPersianDatewith the specified number of days added.This method adds the specified amount to the days field incrementing the month and year fields as necessary to ensure the result remains valid. The result is only invalid if the maximum/minimum year is exceeded.
For example, 1396-12-29 plus one day would result in 1397-01-01.
This instance is immutable and unaffected by this method call.
- Parameters:
daysToAdd- the days to add, may be negative- Returns:
- a
PersianDatebased on this date with the days added, not null - Throws:
java.time.DateTimeException- if the result exceeds the supported date range
-
isLeapYear
public boolean isLeapYear()Returns true ifyearis a leap year in Persian calendar.- Specified by:
isLeapYearin interfacejava.time.chrono.ChronoLocalDate- Returns:
- true if
yearis a leap year in Persian calendar
-
isLeapYear
public static boolean isLeapYear(int year)Returnstrueif the given year is a leap year in Persian calendar.- Parameters:
year- the year to be checked- Returns:
trueif the given year is a leap year in Persian calendar.
-
toGregorian
public java.time.LocalDate toGregorian()Returns an equivalent Gregorian date and time as an instance ofLocalDate. Calling this method has no effect on the object that calls this.- Returns:
- the equivalent Gregorian date as an instance of
LocalDate
-
toEpochDay
public long toEpochDay()- Specified by:
toEpochDayin interfacejava.time.chrono.ChronoLocalDate
-
toJulianDay
public long toJulianDay()Returns number of corresponding julian days. For number of juliand days of PersianDate.of(1396, 8, 6) is 2458054.Calling this method has no effect on this instance.
- Returns:
- number of corresponding julian days
- See Also:
- calendar convertor
-
equals
public boolean equals(java.lang.Object obj)Checks if this date is equal to another date.Compares this
PersianDatewith another ensuring that the date is the same.- Specified by:
equalsin interfacejava.time.chrono.ChronoLocalDate- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- the object to check, null returns false- Returns:
- true if this is equal to the other date
-
hashCode
public int hashCode()A hash code for this persian date.- Specified by:
hashCodein interfacejava.time.chrono.ChronoLocalDate- Overrides:
hashCodein classjava.lang.Object- Returns:
- a suitable hash code
-
toString
public java.lang.String toString()Returns the string representation of this persian date. The string contains of ten characters whose format is "XXXX-YY-ZZ", where XXXX is the year, YY is the month-of-year and ZZ is day-of-month. (Each of the capital characters represents a single decimal digit.)If any of the three parts of this persian date is too small to fill up its field, the field is padded with leading zeros.
- Specified by:
toStringin interfacejava.time.chrono.ChronoLocalDate- Overrides:
toStringin classjava.lang.Object- Returns:
- a suitable representation of this persian date
-