Record Class NaftahDate

java.lang.Object
java.lang.Record
org.daiitech.naftah.builtin.time.NaftahDate
Record Components:
date - the parsed date component
calendar - the optional calendar specification
temporal - the resolved temporal
All Implemented Interfaces:
DateSupport, NaftahTemporal, NaftahTemporalPoint

public record NaftahDate(NaftahDate.Date date, NaftahDate.Calendar calendar, Temporal temporal) extends Record implements NaftahTemporalPoint, DateSupport
Represents an immutable Naftah date composed of:

This record is typically produced after parsing Naftah date expressions and resolving them against a specific Chronology.

It supports both Gregorian and non-Gregorian calendars (e.g. Hijri) and preserves the original Naftah month and calendar names.

Author:
Chakib Daii
  • Field Details

  • Constructor Details

    • NaftahDate

      public NaftahDate(NaftahDate.Date date, NaftahDate.Calendar calendar, Temporal temporal)
      Creates an instance of a NaftahDate record class.
      Parameters:
      date - the value for the date record component
      calendar - the value for the calendar record component
      temporal - the value for the temporal record component
  • Method Details

    • now

      public static NaftahDate now()
      Obtains the current date using the default chronology and the system default time zone.

      This method is equivalent to invoking: now(ChronologyUtils.DEFAULT_CHRONOLOGY, null).

      Returns:
      the current NaftahDate
    • now

      public static NaftahDate now(Chronology chronology)
      Obtains the current date using the specified chronology and the system default time zone.

      The supplied chronology determines how the current day, month, and year are interpreted.

      Parameters:
      chronology - the chronology to use (not null)
      Returns:
      the current NaftahDate
      Throws:
      NullPointerException - if chronology is null
    • now

      public static NaftahDate now(NaftahDate.Calendar calendar)
      Obtains the current date using the specified calendar and the system default time zone.
      Parameters:
      calendar - the calendar to use (not null)
      Returns:
      the current NaftahDate
      Throws:
      NullPointerException - if calendar is null
    • now

      public static NaftahDate now(NaftahTime.ZoneOrOffset zoneOrOffset)
      Obtains the current date using the default chronology and the specified zone or offset.

      This method is useful when resolving the current date relative to a specific time zone or fixed offset.

      Parameters:
      zoneOrOffset - the zone or offset to use, or null to use the system default
      Returns:
      the current NaftahDate
    • now

      public static NaftahDate now(Chronology chronology, NaftahTime.ZoneOrOffset zoneOrOffset)
      Obtains the current date using the specified chronology and zone or offset.
      Parameters:
      chronology - the chronology to use (not null)
      zoneOrOffset - the zone or offset to use, or null to use the system default
      Returns:
      the current NaftahDate
      Throws:
      NullPointerException - if chronology is null
    • now

      public static NaftahDate now(NaftahDate.Calendar calendar, NaftahTime.ZoneOrOffset zoneOrOffset)
      Obtains the current date using the specified calendar and zone or offset.

      The returned NaftahDate represents the current calendar date as resolved by the provided calendar and zone or offset.

      Parameters:
      calendar - the calendar to use (not null)
      zoneOrOffset - the zone or offset to use, or null to use the system default
      Returns:
      the current NaftahDate
      Throws:
      NullPointerException - if calendar is null
    • of

      public static NaftahDate of(NaftahDate.Date date, NaftahDate.Calendar calendar, Temporal temporal)
      Creates a new NaftahDate instance from its parsed components.

      This factory method should be used when the Date, the NaftahDate.Calendar, and the underlying Temporal representation are already known. No additional parsing or chronology resolution is performed.

      Parameters:
      date - the parsed date component (day, month, year) according to the calendar
      calendar - the calendar associated with this date (e.g., Gregorian or Hijri)
      temporal - the resolved temporal representation backing this date
      Returns:
      a new NaftahDate instance combining all components
    • of

      public static NaftahDate of(NaftahDate.Date date, NaftahDate.Calendar calendar)
      Creates a new NaftahDate instance from its parsed components and resolves the underlying Temporal representation automatically.

      This factory method should be used when the Date and the associated NaftahDate.Calendar are already known, but the backing Temporal has not yet been created. The temporal representation is derived using the calendar’s chronology.

      Parameters:
      date - the parsed date component (day, month, year) according to the calendar
      calendar - the calendar associated with this date (e.g., Gregorian or Hijri)
      Returns:
      a new NaftahDate instance combining all components
    • of

      public static NaftahDate of(NaftahDate.Calendar calendar, Temporal temporal)
      Creates a new NaftahDate instance from a Temporal object and a calendar.

      This method extracts the day, month, and year fields from the given temporal object and constructs an NaftahDate using the provided calendar's chronology.

      The temporal must support the following fields:

      Parameters:
      calendar - the calendar that determines the chronology of the date
      temporal - the temporal object containing date information
      Returns:
      a new NaftahDate instance derived from the temporal
      Throws:
      IllegalArgumentException - if the temporal does not support day, month, or year fields
    • of

      public static NaftahDate of(int day, String month, int year)
      Creates a new NaftahDate instance using a day, Naftah month name, and year.

      This method uses the default chronology defined in ChronologyUtils.DEFAULT_CHRONOLOGY.

      Parameters:
      day - the day of the month
      month - the month name (e.g., "رمضان", "يناير")
      year - the year value
      Returns:
      a new NaftahDate instance with the specified components
    • of

      public static NaftahDate of(Chronology chronology, int day, String month, int year)
      Creates a new NaftahDate instance using a specific chronology, day, month name, and year.

      The chronology is used to resolve the month name to its numeric value and to determine leap years.

      Parameters:
      chronology - the chronology to use (e.g., ISO or Hijri)
      day - the day of the month
      month - the month name
      year - the year value
      Returns:
      a new NaftahDate instance with the specified components
    • of

      public static NaftahDate of(NaftahDate.Calendar calendar, int day, String month, int year)
      Creates a new NaftahDate instance using a calendar, day, month name, and year.

      The month name is resolved to a numeric month using the provided calendar's chronology.

      Parameters:
      calendar - the calendar to use
      day - the day of the month
      month - the month name
      year - the year value
      Returns:
      a new NaftahDate instance with the specified components
    • of

      public static NaftahDate of(int day, int month, int year)
      Creates a new NaftahDate instance using numeric day, month, and year values.

      This method uses the default chronology defined in ChronologyUtils.DEFAULT_CHRONOLOGY.

      Parameters:
      day - the day of the month
      month - the numeric month value (1–12)
      year - the year value
      Returns:
      a new NaftahDate instance with the specified components
    • of

      public static NaftahDate of(Chronology chronology, int day, int month, int year)
      Creates a new NaftahDate instance using a specific chronology, numeric day, month, and year.

      The chronology is used for leap year calculations and month name resolution.

      Parameters:
      chronology - the chronology to use (e.g., ISO or Hijri)
      day - the day of the month
      month - the numeric month value (1–12)
      year - the year value
      Returns:
      a new NaftahDate instance with the specified components
    • of

      public static NaftahDate of(NaftahDate.Calendar calendar, int day, int month, int year)
      Creates a new NaftahDate instance using a calendar and numeric day, month, and year.

      The calendar provides the chronology to resolve leap years and month names.

      Parameters:
      calendar - the calendar to use
      day - the day of the month
      month - the numeric month value (1–12)
      year - the year value
      Returns:
      a new NaftahDate instance with the specified components
    • toChronoDate

      public ChronoLocalDate toChronoDate()
      Returns this date interpreted in its calendar chronology.

      The returned ChronoLocalDate is derived from the underlying ISO temporal and the associated Chronology.

      This method must be used whenever calendar-specific fields (year, month, day) are required.

      Returns:
      a ChronoLocalDate in the calendar's chronology
    • toEpochDay

      public long toEpochDay()
      Converts this NaftahDate to the number of days since the epoch (1970-01-01) in the ISO calendar system.
      Specified by:
      toEpochDay in interface DateSupport
      Returns:
      the epoch day count
    • getYear

      public int getYear()
      Returns the year component of this NaftahDate.
      Specified by:
      getYear in interface DateSupport
      Returns:
      the year value
    • getMonthValue

      public int getMonthValue()
      Returns the numeric month value (1–12) of this NaftahDate.
      Specified by:
      getMonthValue in interface DateSupport
      Returns:
      the month value
    • getMonth

      public String getMonth()
      Returns the name of the month for this NaftahDate.
      Specified by:
      getMonth in interface DateSupport
      Returns:
      the month name
    • getDayOfMonth

      public int getDayOfMonth()
      Returns the day-of-month component of this NaftahDate.
      Specified by:
      getDayOfMonth in interface DateSupport
      Returns:
      the day of the month
    • getDayOfYear

      public int getDayOfYear()
      Returns the day-of-year for this NaftahDate according to its chronology.
      Specified by:
      getDayOfYear in interface DateSupport
      Returns:
      the day of the year (1–365 or 1–366 for leap years)
    • getDayOfWeek

      public String getDayOfWeek()
      Returns the day of the week as a string for this NaftahDate.

      The calculation is based on the epoch day of the underlying ChronoLocalDate.

      Specified by:
      getDayOfWeek in interface DateSupport
      Returns:
      the name of the day of the week
    • isLeapYear

      public boolean isLeapYear()
      Checks if the year of this NaftahDate is a leap year in its chronology.
      Specified by:
      isLeapYear in interface DateSupport
      Returns:
      true if the year is a leap year, false otherwise
    • lengthOfMonth

      public int lengthOfMonth()
      Returns the length of the month in days for this NaftahDate.

      The length is determined according to the chronology of the calendar.

      Specified by:
      lengthOfMonth in interface DateSupport
      Returns:
      the number of days in the month
    • lengthOfYear

      public int lengthOfYear()
      Returns the length of the year in days for this NaftahDate.

      The number of days depends on the chronology of the date:

      • For Gregorian years (ISO chronology), leap years return 366 days, and non-leap years return 365 days.
      • For Hijri years, leap years return 355 days, and non-leap years return 354 days.

      Specified by:
      lengthOfYear in interface DateSupport
      Returns:
      the number of days in the year according to its chronology
    • plus

      public NaftahDate plus(NaftahTemporalAmount naftahTemporalAmount)
      Returns a new NaftahDate obtained by adding the given Naftah temporal amount to this date.
      Specified by:
      plus in interface NaftahTemporalPoint
      Parameters:
      naftahTemporalAmount - the temporal amount to add
      Returns:
      a new NaftahDate instance
    • plusYears

      public NaftahDate plusYears(long yearsToAdd)
      Returns a new NaftahDate with the specified number of years added.
      Specified by:
      plusYears in interface DateSupport
      Parameters:
      yearsToAdd - the number of years to add, may be negative
      Returns:
      a new NaftahDate instance
    • plusMonths

      public NaftahDate plusMonths(long monthsToAdd)
      Returns a new NaftahDate with the specified number of months added.
      Specified by:
      plusMonths in interface DateSupport
      Parameters:
      monthsToAdd - the number of months to add, may be negative
      Returns:
      a new NaftahDate instance
    • plusWeeks

      public NaftahDate plusWeeks(long weeksToAdd)
      Returns a new NaftahDate with the specified number of weeks added.
      Specified by:
      plusWeeks in interface DateSupport
      Parameters:
      weeksToAdd - the number of weeks to add, may be negative
      Returns:
      a new NaftahDate instance
    • plusDays

      public NaftahDate plusDays(long daysToAdd)
      Returns a new NaftahDate with the specified number of days added.
      Specified by:
      plusDays in interface DateSupport
      Parameters:
      daysToAdd - the number of days to add, may be negative
      Returns:
      a new NaftahDate instance
    • minus

      public NaftahDate minus(NaftahTemporalAmount naftahTemporalAmount)
      Returns a new NaftahDate obtained by subtracting the given Naftah temporal amount from this date.
      Specified by:
      minus in interface NaftahTemporalPoint
      Parameters:
      naftahTemporalAmount - the temporal amount to subtract
      Returns:
      a new NaftahDate instance
    • minusYears

      public NaftahDate minusYears(long yearsToSubtract)
      Returns a new NaftahDate with the specified number of years subtracted.
      Specified by:
      minusYears in interface DateSupport
      Parameters:
      yearsToSubtract - the number of years to subtract, may be negative
      Returns:
      a new NaftahDate instance
    • minusMonths

      public NaftahDate minusMonths(long monthsToSubtract)
      Returns a new NaftahDate with the specified number of months subtracted.
      Specified by:
      minusMonths in interface DateSupport
      Parameters:
      monthsToSubtract - the number of months to subtract, may be negative
      Returns:
      a new NaftahDate instance
    • minusWeeks

      public NaftahDate minusWeeks(long weeksToSubtract)
      Returns a new NaftahDate with the specified number of weeks subtracted.
      Specified by:
      minusWeeks in interface DateSupport
      Parameters:
      weeksToSubtract - the number of weeks to subtract, may be negative
      Returns:
      a new NaftahDate instance
    • minusDays

      public NaftahDate minusDays(long daysToSubtract)
      Returns a new NaftahDate with the specified number of days subtracted.
      Specified by:
      minusDays in interface DateSupport
      Parameters:
      daysToSubtract - the number of days to subtract, may be negative
      Returns:
      a new NaftahDate instance
    • compute

      NaftahDate compute(NaftahTemporalAmount arabicTemporalAmount, Function<TemporalAmount,Temporal> computeFunction)
      Computes a new NaftahDate by applying the given temporal computation.

      Date-based arithmetic supports:

      Durations shorter than 24 hours are not permitted for date-only values.

      Parameters:
      arabicTemporalAmount - the temporal amount to apply
      computeFunction - the temporal computation function
      Returns:
      a new NaftahDate instance
      Throws:
      IllegalArgumentException - if the operation is not supported
    • toString

      public String toString()
      Returns a string representation of this NaftahDate in the format: "date calendar".

      If a NaftahDate.Calendar component is present, it is appended after the date. Otherwise, only the date is returned.

      Specified by:
      toString in class Record
      Returns:
      a formatted string representing the Naftah date and optional calendar
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • date

      public NaftahDate.Date date()
      Returns the value of the date record component.
      Returns:
      the value of the date record component
    • calendar

      public NaftahDate.Calendar calendar()
      Returns the value of the calendar record component.
      Returns:
      the value of the calendar record component
    • temporal

      public Temporal temporal()
      Returns the value of the temporal record component.
      Specified by:
      temporal in interface NaftahTemporalPoint
      Returns:
      the value of the temporal record component