Class TemporalUtils
Temporal instances
from Naftah date and time components.
Provides methods to create:
LocalTime,OffsetTime, andZonedDateTimefromNaftahTime.TimeChronoLocalDatefrom Naftah date components andChronologyLocalDateTime,OffsetDateTime, andZonedDateTimefrom Naftah date-time components
Handles conversion between 12-hour and 24-hour formats, applies AM/PM logic, and supports both ISO and Hijrah (Islamic) chronologies.
- Author:
- Chakib Daii
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePrivate constructor to prevent instantiation. -
Method Summary
Modifier and TypeMethodDescriptionstatic NTupleComputes the temporal difference between twoTemporalinstances.static intCompares twoPeriodinstances by years, then months, then days.private static ChronoLocalDatecreateChronoLocalDate(int day, int monthValue, int year, Chronology chronology) Creates aChronoLocalDateusing the specified day, month, year, and chronology.static LocalDatecreateDate(int day, int monthValue, int year, Chronology chronology) Creates aLocalDate(ISO calendar) from the specified day, month, year, and chronology.static TemporalcreateDateTime(int day, int monthValue, int year, Chronology chronology, int hour24, Integer minute, Integer second, Integer nano, ZoneId zoneId) Creates aTemporalrepresenting a date and time with optional time zone or offset.static TemporalcreateDateTime(NaftahDate.Date date, NaftahDate.Calendar calendar, NaftahTime.Time time, NaftahTime.ZoneOrOffset zoneOrOffset) Creates aTemporalrepresenting the given Naftah date and time, optionally including a time zone or offset.static LocalDateTimecreateLocalDateTime(Chronology chronology, Supplier<ChronoLocalDateTime<?>> chronoLocalDateTimeSupplier, Supplier<LocalDateTime> localDateTimeSupplier) Creates aLocalDateTimefrom a chronology and suppliers for ISO and non-ISO dates.static OffsetDateTimecreateOffsetDateTime(Chronology chronology, Supplier<ChronoLocalDateTime<?>> chronoLocalDateTimeSupplier, Supplier<LocalDateTime> localDateTimeSupplier, ZoneOffset offset) Creates anOffsetDateTimefrom a chronology, suppliers, and aZoneOffset.static OffsetTimecreateOffsetTime(LocalTime time, ZoneId zoneId) static TemporalcreateTime(int hour24, Integer minute, Integer second, Integer nano, ZoneId zoneId) Creates aTemporalrepresenting a time with explicit components.static TemporalcreateTime(int hour24, Integer minute, Integer second, Integer nano, NaftahTime.ZoneOrOffset zoneOrOffset) Creates aTemporalrepresenting a time using explicit components and an optional time zone or offset.static TemporalcreateTime(LocalTime time, ZoneId zoneId) static TemporalcreateTime(NaftahTime.Time time, NaftahTime.ZoneOrOffset zoneOrOffset) Creates aTemporalrepresenting the given Naftah time, optionally including a time zone or offset.static NTuplecreateTimeOfNanoOfDay(long nanoOfDay, NaftahTime.ZoneOrOffset zoneOrOffset) static NTuplecreateTimeOfSecondOfDay(long secondOfDay, NaftahTime.ZoneOrOffset zoneOrOffset) static ZonedDateTimecreateZonedDateTime(Chronology chronology, Supplier<ChronoLocalDateTime<?>> chronoLocalDateTimeSupplier, Supplier<LocalDateTime> localDateTimeSupplier, ZoneId zoneId) Creates aZonedDateTimefrom a chronology, suppliers, and aZoneId.static ChronoLocalDatecurrentDate(Chronology chronology, ZoneId zoneId) static TemporalcurrentDate(NaftahDate.Calendar calendar, NaftahTime.ZoneOrOffset zoneOrOffset) Returns the currentTemporalfor the specifiedNaftahDate.Calendarand optionalNaftahTime.ZoneOrOffset.static TemporalcurrentTime(ZoneId zoneId) static TemporalcurrentTime(NaftahTime.ZoneOrOffset zoneOrOffset) Returns the currentTemporalrepresenting the time in the specified zone or offset.static intConverts an hour and AM/PM indicator to 24-hour format.private static booleanisHijrah(Chronology chronology) Determines whether the givenChronologyrepresents the Hijrah (Islamic) calendar.static booleanReturnstrueif the given AM/PM marker represents PM.static intparseFractionToNanos(String fraction) Parses the fractional part after seconds into nanoseconds.static intparseMillisFraction(String fraction) Parses the fractional part after seconds as milliseconds.static Instantprivate static LocalDateTimeConverts aTemporalinstance to aLocalDateTime.
-
Constructor Details
-
TemporalUtils
private TemporalUtils()Private constructor to prevent instantiation. Always throws aNaftahBugErrorwhen called.
-
-
Method Details
-
createTime
Creates aTemporalrepresenting the given Naftah time, optionally including a time zone or offset.- Parameters:
time- the Naftah time componentzoneOrOffset- the optional time zone or offset- Returns:
- a Temporal representing the time, either
LocalTime,OffsetTime, orZonedDateTimedepending on the zone/offset
-
createTimeOfNanoOfDay
Creates aLocalTimeand a correspondingTemporalfrom the total number of nanoseconds since midnight.The returned tuple contains:
- The computed
LocalTime - A
Temporalthat may beLocalTime,OffsetTime, orZonedDateTimedepending on the zone
- The computed
-
createTimeOfSecondOfDay
public static NTuple createTimeOfSecondOfDay(long secondOfDay, NaftahTime.ZoneOrOffset zoneOrOffset) Creates aLocalTimeand a correspondingTemporalfrom the total number of seconds since midnight.The returned tuple contains:
- The computed
LocalTime - A
Temporalthat may beLocalTime,OffsetTime, orZonedDateTimedepending on the zone
- The computed
-
createTime
public static Temporal createTime(int hour24, Integer minute, Integer second, Integer nano, NaftahTime.ZoneOrOffset zoneOrOffset) Creates aTemporalrepresenting a time using explicit components and an optional time zone or offset.Any null minute, second, or nanosecond value will default to
0.- Parameters:
hour24- the hour in 24-hour format (0–23)minute- the minute of the hour, may be nullsecond- the second of the minute, may be nullnano- the nanosecond part, may be nullzoneOrOffset- the optional time zone or offset, may be null- Returns:
- a
Temporalrepresenting the specified time, never null
-
createTime
public static Temporal createTime(int hour24, Integer minute, Integer second, Integer nano, ZoneId zoneId) Creates aTemporalrepresenting a time with explicit components. -
currentTime
Returns the currentTemporalrepresenting the time in the specified zone or offset.- Parameters:
zoneOrOffset- the optionalNaftahTime.ZoneOrOffset, may be null- Returns:
- a
Temporalrepresenting the current time in the given zone or offset
-
currentTime
-
createTime
Creates aTemporalfrom aLocalTimeand an optionalZoneId.If a
ZoneIdis provided:- If it is a
ZoneOffset, returns anOffsetTime. - Otherwise, returns a
ZonedDateTimeanchored to today in that zone.
- If it is a
-
createChronoLocalDate
private static ChronoLocalDate createChronoLocalDate(int day, int monthValue, int year, Chronology chronology) Creates aChronoLocalDateusing the specified day, month, year, and chronology.This method preserves the original chronology, so it can represent dates in non-ISO calendars such as
HijrahChronology.- Parameters:
day- the day of the month (1–31, depending on month and chronology)monthValue- the month number (1–12)year- the year in the specified chronologychronology- theChronologyto use (e.g.,IsoChronologyor * HijrahChronology)- Returns:
- a
ChronoLocalDaterepresenting the specified date in the given chronology
-
createDate
Creates aLocalDate(ISO calendar) from the specified day, month, year, and chronology.If a non-ISO chronology is provided (e.g., HijrahChronology), the date is converted to the ISO calendar system. This is useful for storing or processing dates in a standardized ISO format while preserving the original calendar's meaning.
- Parameters:
day- the day of the month (1–31)monthValue- the month number (1–12)year- the year in the specified chronologychronology- theChronologyto use (e.g., ISO or Hijrah)- Returns:
- a
LocalDatein the ISO calendar representing the same date as the specified chronology
-
currentDate
public static Temporal currentDate(NaftahDate.Calendar calendar, NaftahTime.ZoneOrOffset zoneOrOffset) Returns the currentTemporalfor the specifiedNaftahDate.Calendarand optionalNaftahTime.ZoneOrOffset.- Parameters:
calendar- the Naftah calendarzoneOrOffset- the optional zone or offset, may be null- Returns:
- a
Temporalrepresenting the current date in the given calendar and zone
-
currentDate
- Parameters:
chronology- the chronology to use (e.g., ISO or Hijrah)zoneId- the optionalZoneId, may be null- Returns:
- a
ChronoLocalDaterepresenting the current date in the given chronology and zone
-
createDateTime
public static Temporal createDateTime(NaftahDate.Date date, NaftahDate.Calendar calendar, NaftahTime.Time time, NaftahTime.ZoneOrOffset zoneOrOffset) Creates aTemporalrepresenting the given Naftah date and time, optionally including a time zone or offset.- Parameters:
date- the Naftah date componentcalendar- the Naftah calendar componenttime- the Naftah time componentzoneOrOffset- the optional time zone or offset- Returns:
- a Temporal representing the date and time
-
createDateTime
public static Temporal createDateTime(int day, int monthValue, int year, Chronology chronology, int hour24, Integer minute, Integer second, Integer nano, ZoneId zoneId) Creates aTemporalrepresenting a date and time with optional time zone or offset.This method constructs the appropriate temporal object based on the inputs:
- If a
ZoneOffsetis provided, returns anOffsetDateTime. - If a
ZoneIdis provided, returns aZonedDateTime. - If no zone/offset is provided, returns a
LocalDateTime.
Supports both ISO and non-ISO chronologies (e.g., Hijrah) and applies the correct conversions.
- Parameters:
day- the day of the monthmonthValue- the month (1–12)year- the yearchronology- the chronology to use (ISO, Hijrah, etc.)hour24- the hour in 24-hour formatminute- the minute of the hour, may be nullsecond- the second of the minute, may be nullnano- the nanosecond fraction, may be nullzoneId- the optionalZoneIdorZoneOffset, may be null- Returns:
- a
Temporalrepresenting the date and time with optional zone/offset
- If a
-
isPM
Returnstrueif the given AM/PM marker represents PM.- Parameters:
ampm- the AM/PM marker string- Returns:
- true if PM, false if AM
-
createOffsetTime
Creates anOffsetTimefrom aLocalTimeandZoneId.The actual offset is determined for the given zone at a fixed epoch date.
- Parameters:
time- the local timezoneId- the zone to apply- Returns:
- an OffsetTime representing the time at the given zone offset
-
createLocalDateTime
public static LocalDateTime createLocalDateTime(Chronology chronology, Supplier<ChronoLocalDateTime<?>> chronoLocalDateTimeSupplier, Supplier<LocalDateTime> localDateTimeSupplier) Creates aLocalDateTimefrom a chronology and suppliers for ISO and non-ISO dates.For Hijrah chronology, converts to ISO
LocalDateTime; otherwise uses ISO directly.- Parameters:
chronology- the chronology of the datechronoLocalDateTimeSupplier- supplier for ChronoLocalDateTimelocalDateTimeSupplier- supplier for ISO LocalDateTime- Returns:
- the resulting LocalDateTime
-
createOffsetDateTime
public static OffsetDateTime createOffsetDateTime(Chronology chronology, Supplier<ChronoLocalDateTime<?>> chronoLocalDateTimeSupplier, Supplier<LocalDateTime> localDateTimeSupplier, ZoneOffset offset) Creates anOffsetDateTimefrom a chronology, suppliers, and aZoneOffset.For Hijrah chronology, converts to ISO and applies the offset; otherwise uses ISO directly.
- Parameters:
chronology- the chronology of the datechronoLocalDateTimeSupplier- supplier for ChronoLocalDateTimelocalDateTimeSupplier- supplier for ISO LocalDateTimeoffset- the zone offset to apply- Returns:
- the resulting OffsetDateTime
-
createZonedDateTime
public static ZonedDateTime createZonedDateTime(Chronology chronology, Supplier<ChronoLocalDateTime<?>> chronoLocalDateTimeSupplier, Supplier<LocalDateTime> localDateTimeSupplier, ZoneId zoneId) Creates aZonedDateTimefrom a chronology, suppliers, and aZoneId.For Hijrah chronology, converts to ISO and applies the zone; otherwise uses ISO directly.
- Parameters:
chronology- the chronology of the datechronoLocalDateTimeSupplier- supplier for ChronoLocalDateTimelocalDateTimeSupplier- supplier for ISO LocalDateTimezoneId- the time zone to apply- Returns:
- the resulting ZonedDateTime
-
getHour24
Converts an hour and AM/PM indicator to 24-hour format.If the hour is in 12-hour format, AM/PM is applied to produce a 24-hour value.
- Parameters:
hour- the hour (1–12 or 0–23)isPM- true if PM, false if AM, null if 24-hour format- Returns:
- the hour in 24-hour format
-
between
Computes the temporal difference between twoTemporalinstances.The result is returned as an
NTuplecontaining:- Parameters:
start- the start temporal pointend- the end temporal point- Returns:
- an
NTuplecontaining the computed temporal amount(s) - Throws:
IllegalArgumentException- if the temporal type is not supported
-
toLocalDateTime
Converts aTemporalinstance to aLocalDateTime.Supported types:
LocalDateTime,LocalDate,LocalTime,ZonedDateTime,OffsetDateTime, andInstant.Conversion rules:
LocalDate→ start of dayLocalTime→ combined with the current system dateZonedDateTime/OffsetDateTime→ zone/offset discardedInstant→ converted using the system default time zone
- Parameters:
t- the temporal value to convert- Returns:
- a
LocalDateTimerepresentation of the temporal value - Throws:
IllegalArgumentException- if the temporal type is not supported
-
toInstant
Converts aTemporalinstance to anInstant.Supported types:
Instant,ZonedDateTime,OffsetDateTime,LocalDateTime,LocalDate, andLocalTime.Conversion rules:
ZonedDateTime/OffsetDateTime→ converted directlyLocalDateTime→ interpreted using the system default time zoneLocalDate→ start of day in the system default time zoneLocalTime→ combined with the current system date and zone
- Parameters:
t- the temporal value to convert- Returns:
- an
Instantrepresenting the temporal value - Throws:
IllegalArgumentException- if the temporal type is not supported
-
compare
Compares twoPeriodinstances by years, then months, then days.Comparison is performed lexicographically: years → months → days.
- Parameters:
p1- the first period to comparep2- the second period to compare- Returns:
- a negative integer, zero, or a positive integer as
p1is less than, equal to, or greater thanp2
-
parseMillisFraction
Parses the fractional part after seconds as milliseconds.The fraction must contain at most 3 digits:
.5 -> 500 ms.25 -> 250 ms.125 -> 125 ms
- Parameters:
fraction- the fractional part without the dot- Returns:
- milliseconds value (0–999)
- Throws:
IllegalArgumentException- if the fraction exceeds 3 digits
-
parseFractionToNanos
Parses the fractional part after seconds into nanoseconds.Supported formats:
- 1–3 digits → milliseconds
- 4–9 digits → nanoseconds
- Parameters:
fraction- the fractional part without the dot- Returns:
- nanoseconds value (0–999,999,999)
- Throws:
IllegalArgumentException- if the fraction exceeds 9 digits
-
isHijrah
Determines whether the givenChronologyrepresents the Hijrah (Islamic) calendar.This method compares the provided chronology against the system-supported Hijrah chronology used by the library.
- Parameters:
chronology- the chronology to test, may benull- Returns:
trueif the chronology is the Hijrah calendar,falseotherwise
-