Add code for Gregorian date to a Hijri date conversion (#15696)

Co-authored-by: rajatgarg <rajatgarg@adobe.com>
This commit is contained in:
Rajat Garg 2024-01-21 06:44:00 +05:30 committed by GitHub
parent 320c1f3064
commit f0ef5ef472
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 94 additions and 0 deletions

View File

@ -25,6 +25,12 @@
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>com.github.msarhan</groupId>
<artifactId>ummalqura-calendar</artifactId>
<version>${ummalqura-calendar.version}</version>
</dependency>
</dependencies>
<build>
@ -45,6 +51,7 @@
<properties>
<joda-time.version>2.12.5</joda-time.version>
<ummalqura-calendar.version>2.0.2</ummalqura-calendar.version>
</properties>
</project>

View File

@ -0,0 +1,34 @@
package com.baeldung.gregoriantohijri;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.chrono.ChronoLocalDate;
import java.time.chrono.HijrahChronology;
import java.time.chrono.HijrahDate;
import java.util.GregorianCalendar;
import org.joda.time.chrono.IslamicChronology;
import com.github.msarhan.ummalqura.calendar.UmmalquraCalendar;
public class GregorianToHijriDateConverter {
public static HijrahDate usingHijrahChronology(LocalDate gregorianDate) {
HijrahChronology hijrahChronology = HijrahChronology.INSTANCE;
ChronoLocalDate hijriChronoLocalDate = hijrahChronology.date(gregorianDate);
return HijrahDate.from(hijriChronoLocalDate);
}
public static HijrahDate usingFromMethod(LocalDate gregorianDate) {
return HijrahDate.from(gregorianDate);
}
public static org.joda.time.DateTime usingJodaDate(org.joda.time.DateTime gregorianDate) {
return gregorianDate.withChronology(IslamicChronology.getInstance());
}
public static UmmalquraCalendar usingUmmalquraCalendar(GregorianCalendar gregorianCalendar) throws ParseException {
UmmalquraCalendar hijriCalendar = new UmmalquraCalendar();
hijriCalendar.setTime(gregorianCalendar.getTime());
return hijriCalendar;
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.gregoriantohijri;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.chrono.HijrahDate;
import java.time.temporal.ChronoField;
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;
import com.github.msarhan.ummalqura.calendar.UmmalquraCalendar;
public class GregorianToHijriDateConverterUnitTest {
@Test
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));
}
@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));
}
@Test
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());
}
@Test
void givenGregorianDate_whenUsingUmmalquraCalendar_thenConvertHijriDate() throws ParseException {
GregorianCalendar gregorianCalendar = new GregorianCalendar(2013, Calendar.MARCH, 31);
UmmalquraCalendar ummalquraCalendar = GregorianToHijriDateConverter.usingUmmalquraCalendar(gregorianCalendar);
assertEquals(1434, ummalquraCalendar.get(Calendar.YEAR));
assertEquals(5, ummalquraCalendar.get(Calendar.MONTH) + 1);
assertEquals(19, ummalquraCalendar.get(Calendar.DAY_OF_MONTH));
}
}