Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

The Gregorian and Hijri calendars represent two distinct systems for measuring time.

In this tutorial, we’ll look at various approaches to converting a Gregorian Date to a Hijri Date.

2. Gregorian vs. Hijri Calendar

Let’s understand the difference between the Gregorian and Hijri calendars. The Gregorian calendar follows the solar year, consisting of 12 months with fixed lengths. The Hijri Calendar follows the lunar year and has 12 months alternating between 29 and 30 days.

In the Hijri calendar, the length of each month depends on the period of a complete moon revolution around the Earth. The Gregorian calendar consists of 365 or 366 days, whereas the Hijri calendar has 354 or 355 days. It means a Hijri year is approximately 11 days shorter than a Gregorian year.

3. Using the HijrahDate Class

In this approach, we’ll use the HijrahDate class from the java.time.chrono package. This class was introduced in Java 8 for modern date and time operations. It provides multiple methods to create and manipulate Hijri dates.

3.1. Using the from() Method

We’ll use the from() method of the HijrahDate class to convert a date from the Gregorian to the Hijri calendar. This method takes a LocalDate object representing Gregorian date as an input and returns a HijriDate object:

public HijrahDate usingFromMethod(LocalDate gregorianDate) {
    return HijrahDate.from(gregorianDate);
}

Now, let’s run our test:

void givenGregorianDate_whenUsingFromMethod_thenConvertHijriDate() {
    LocalDate gregorianDate = LocalDate.of(2013, 3, 31);
    HijrahDate hijriDate = GregorianToHijriDateConverter.usingFromMethod(gregorianDate);
    assertEquals(1434, hijriDate.get(ChronoField.YEAR));
    assertEquals(5, hijriDate.get(ChronoField.MONTH_OF_YEAR));
    assertEquals(19, hijriDate.get(ChronoField.DAY_OF_MONTH));
}

3.2. Using the HijrahChronology Class

In this approach, we’ll use the java.time.chrono.HijrahChronology class, which represents the Hijri (Islamic) calendar system.

The HijrahChoronology.INSTANCE method creates an instance of the Hijri calendar system. We’ll use it to create the ChronoLocalDate object to convert a Gregorian date to a Hijri date:

public HijrahDate usingHijrahChronology(LocalDate gregorianDate) {
    HijrahChronology hijrahChronology = HijrahChronology.INSTANCE;
    ChronoLocalDate hijriChronoLocalDate = hijrahChronology.date(gregorianDate);
    return HijrahDate.from(hijriChronoLocalDate);
}

Now, let’s test this approach:

void givenGregorianDate_whenUsingHijrahChronologyClass_thenConvertHijriDate() {
    LocalDate gregorianDate = LocalDate.of(2013, 3, 31);
    HijrahDate hijriDate = GregorianToHijriDateConverter.usingHijrahChronology(gregorianDate);
    assertEquals(1434, hijriDate.get(ChronoField.YEAR));
    assertEquals(5, hijriDate.get(ChronoField.MONTH_OF_YEAR));
    assertEquals(19, hijriDate.get(ChronoField.DAY_OF_MONTH));
}

4. Using Joda-Timе

Joda-Timе is a popular datе and timе manipulation library for Java, offering an altеrnativе to thе standard Java Datе and Timе API with a morе intuitivе intеrfacе.

In Joda-Time, the IslamicChronology class represents the Hijri(Islamic) calendar. We’ll be using the DateTime’s withChronology() method with an IslamicChornology instance to convert the Gregorian date to a Hijri date:

public DateTime usingJodaDate(DateTime gregorianDate) {
    return gregorianDate.withChronology(IslamicChronology.getInstance());
}

Now, let’s test this approach:

void givenGregorianDate_whenUsingJodaDate_thenConvertHijriDate() {
    DateTime gregorianDate = new DateTime(2013, 3, 31, 0, 0, 0);
    DateTime hijriDate = GregorianToHijriDateConverter.usingJodaDate(gregorianDate);
    assertEquals(1434, hijriDate.getYear());
    assertEquals(5, hijriDate.getMonthOfYear());
    assertEquals(19, hijriDate.getDayOfMonth());
}

5. Using the UmmalquraCalendar Class

The ummalqura-calendar library has the UmmalquraCalendar class, which is derived from Java 8. To include the ummalqura-calendar library, we need to add the following dependency:

<dependency>
    <groupId>com.github.msarhan</groupId>
    <artifactId>ummalqura-calendar</artifactId>
    <version>2.0.2</version>
</dependency>

We’ll use its setTime() method to perform the Gregorian to a Hijri date conversion:

public UmmalquraCalendar usingUmmalquraCalendar(GregorianCalendar gregorianCalendar) throws ParseException {
    UmmalquraCalendar hijriCalendar = new UmmalquraCalendar();
    hijriCalendar.setTime(gregorianCalendar.getTime());
    return hijriCalendar;
}

Now, let’s test this approach:

void givenGregorianDate_whenUsingUmmalquraCalendar_thenConvertHijriDate() throws ParseException {
    GregorianCalendar gregorianCalenar = new GregorianCalendar(2013, Calendar.MARCH, 31);
    UmmalquraCalendar ummalquraCalendar = GregorianToHijriDateConverter.usingUmmalquraCalendar(gregorianCalenar);
    assertEquals(1434, ummalquraCalendar.get(Calendar.YEAR));
    assertEquals(5, ummalquraCalendar.get(Calendar.MONTH) + 1);
    assertEquals(19, ummalquraCalendar.get(Calendar.DAY_OF_MONTH));
}

6. Conclusion

In this tutorial, we’ve discussed various ways to convert a Gregorian date to a Hijri date.

As always, the code used in the examples is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.