Class MonthUtils

java.lang.Object
org.daiitech.naftah.utils.time.MonthUtils

public final class MonthUtils extends Object
Utility class for converting month names to numeric month values.

This class supports both Gregorian and Hijri calendars.

Methods include:

Throws IllegalArgumentException for unknown month names.

Author:
Chakib Daii
  • Constructor Details

    • MonthUtils

      private MonthUtils()
      Private constructor to prevent instantiation. Always throws a NaftahBugError when called.
  • Method Details

    • getGregorianMonth

      public static Month getGregorianMonth(String monthName)
      Converts a Gregorian month name to a Month enum.
      Parameters:
      monthName - the month name
      Returns:
      the corresponding Month enum
      Throws:
      IllegalArgumentException - if the month name is not recognized
    • getGregorianMonthName

      public static String getGregorianMonthName(int month)
      Returns the name of a Gregorian month given its numeric value.

      The returned name corresponds to the commonly used variant (e.g., "جانفي", "فيفري", "أفريل").

      Parameters:
      month - the Gregorian month number (1–12)
      Returns:
      the month name
      Throws:
      IllegalArgumentException - if the month number is not in the range 1–12
    • getHijriMonth

      public static int getHijriMonth(String monthName)
      Converts an Hijri month name to its corresponding numeric value (1–12).
      Parameters:
      monthName - the Hijri month name
      Returns:
      the month number (1 for Muharram, 12 for Dhu al-Hijjah)
      Throws:
      IllegalArgumentException - if the month name is not recognized
    • getHijriMonthName

      public static String getHijriMonthName(int month)
      Returns the Hijri month name corresponding to the given numeric value.

      The mapping follows the standard Hijri calendar ordering (1 = Muharram, 12 = Dhu al-Hijjah).

      Parameters:
      month - the Hijri month number (1–12)
      Returns:
      the Hijri month name
      Throws:
      IllegalArgumentException - if the month number is not in the range 1–12
    • monthNameToNumber

      public static int monthNameToNumber(String monthName, Chronology chronology)
      Converts a month name to its numeric value according to the given chronology.

      If the chronology is HijrahChronology, uses Hijri months; otherwise uses Gregorian months.

      Parameters:
      monthName - the month name
      chronology - the chronology to determine the calendar type
      Returns:
      the numeric month value
      Throws:
      IllegalArgumentException - if the month name is not recognized
    • monthNumberToName

      public static String monthNumberToName(int month, Chronology chronology)
      Converts a numeric month value to its month name according to the given chronology.

      If the chronology is HijrahChronology, the returned name is a Hijri month name. Otherwise, a Gregorian month name is returned.

      Parameters:
      month - the month number (1–12)
      chronology - the chronology used to determine the calendar system
      Returns:
      the month name corresponding to the given month number
      Throws:
      IllegalArgumentException - if the month number is invalid or unsupported
    • getMonthLength

      public static int getMonthLength(int month, int year, Chronology chronology)
      Returns the number of days in a given month for the specified chronology and year.
      Parameters:
      month - the month number (1–12)
      year - the year
      chronology - the chronology (Gregorian or Hijri)
      Returns:
      the number of days in the month
      Throws:
      IllegalArgumentException - if month is not in 1–12