Convert Gregorian to Hijri Date in Java – 用 Java 将公历日期转换为回历日期

最后修改: 2024年 1月 29日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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

2.公历与回历

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.

让我们来了解一下格里高利历和回历之间的区别。格里高利历遵循阳历年,由长度固定的 12 个月组成。希吉里历遵循阴历年,有 12 个月,29 天和 30 天交替出现。

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.

在回历中,每个月的长度取决于月亮绕地球一周的时间。公历有 365 或 366 天,而回历有 354 或 355 天。这意味着回历年比公历年短大约 11 天。

3. Using the HijrahDate Class

3.使用 HijrahDate

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.

在这种方法中,我们将使用 java.time.chrono 包中的 HijrahDate 类。该类在 Java 8 中引入,用于现代日期和时间操作。它提供了多种方法来创建和操作回历日期。

3.1. Using the from() Method

3.1.使用 from() 方法

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:

我们将使用HijrahDate类的the from() 方法将日期从公历转换为回历。该方法将 LocalDate 对象(代表公历日期)作为输入,并返回 HijriDate 对象:

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

3.2.使用 HijrahChronology

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

在这种方法中,我们将使用表示回历(伊斯兰教)日历系统的 the java.time.chrono.HijrahChronology 类。

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:

HijrahChoronology.INSTANCE方法创建了一个回历系统实例。我们将用它来创建 ChronoLocalDate对象,以便将公历日期转换为回历日期:

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е

4.使用Joda-Timе</em

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е.

Joda-Timе是一个流行的 Java 数据和时间操作库,它提供了一个与标准 Java 数据和时间 API 不同的、更直观的函数库。

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:

Joda-Time 中,IslamicChronology 类代表回历(伊斯兰)。我们将使用 DateTime 的 withChronology() 方法和一个IslamicChornology 实例来将公历日期转换为回历日期

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

5.使用 UmmalquraCalendar

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:

ummalqura-calendar 库中有 UmmalquraCalendar 类,该类源自 Java 8。要包含 ummalqura-calendar 库,我们需要添加以下 依赖关系

<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:

我们将使用它的 setTime() 方法来执行公历日期到回历日期的转换:

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

6.结论

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.

一如既往,示例中使用的代码可在 GitHub 上获取。