Search code examples
javadatedatetimejodatimehijri

JodaTime (Java): Hijri to Gregorian conversion inaccurate?


I am currently implementing a calendar converter which, given a Hijri input date and time, outputs the corresponding Gregorian date and time using the JodaTime library. However, the corresponding Gregorian time is not accurate. No matter what Hijri time I input in the same day, the resulting Gregorian date is the same. This seems strange, considering that each Hijri day spans two Gregorian days, sunset to sunset, as stated in this answer (https://islam.stackexchange.com/questions/71850/can-a-day-in-the-gregorian-calendar-correspond-to-two-different-days-in-the-isla).

For example, below is the code which converts from a Hijri DateTime (current day, can specify a different time in the day) to the corresponding Gregorian DateTime:

Chronology iso = ISOChronology.getInstanceUTC();
Chronology hijri = IslamicChronology.getInstanceUTC();

DateTime currentDate = DateTime.now();
int year = currentDate.getYear();
int month = currentDate.getMonthOfYear();
int day = currentDate.getDayOfMonth();

int customHr = 23;
int customMin = 59;
int customSec = 59;

DateTime todayIso = new DateTime(year, month, day, customHr, customMin, customSec, 0, iso);
LocalDateTime todayHijri = new LocalDateTime(todayIso, hijri);
System.out.println(todayHijri.toString());

Below are the results:

Hijri DateTime Input: 8/24/2021 at final second (23:59)
Gregorian DateTime Output: 1443-01-15T23:59:59.000

Hijri DateTime Input: 8/24/2021 at first second (0:01)
Gregorian DateTime Output: 1443-01-15T00:00:01.000

However, when I run this piece of code which converts from a Gregorian DateTime to a Hijri DateTime:

Chronology hijri = IslamicChronology.getInstanceUTC();

int customHour = 0;
int customMin = 0;
int customSec = 1;

DateTime todayIso = new DateTime(2021, 8, 24, customHour, customMin, customSec);
LocalDateTime todayHijri = new LocalDateTime(todayIso, hijri);
System.out.println(todayHijri); 

These are the results I get, using the outputs of the Gregorian to Hijri conversion as inputs. As you can see the outputted Hijri DateTimes are different than the input Hijri DateTimes in the Hijri to Gregorian conversion, which means something is wrong.

Gregorian DateTime Input: 8-24-2021 at 0:01
Hijri DateTime Output: 1443-01-15T07:00:01.000

Gregorian DateTime Input: 8-24-2021 at 23:59:59
Hijri DateTime Output: 1443-01-16T06:59:59.000

Solution

  • From: http://joda-time.sourceforge.net/cal_islamic.html

    A day in the Islamic calendar begins at sunset on the previous 'day'. Joda-Time does not model this, thus times and date rollover follow standard ISO definitions, in other words starting at midnight

    As Joda-Time is in maintenance,

    See: How to convert from Hijri Date to Georgian Date and vice versa for options.