Hijri to Gregorian date conversion in Java 8

One of the main features released in Java SE 8 is the new date time api (java.time package). It has been a long standing pain for java developers to manipulate dates using the cumbersome Calendar api. Usually developers would go for external date time libraries like JodaTime to get over the issues with the default java Calendar api.

The good news for us in the islamic world is that Java SE 8 includes support for Hijri Calendar (Umm
Alqurra) by default. Date conversions from and to Hirji couldn’t be easier. The code snippet below shows an example on how to do this Java 8.


import java.time.chrono.HijrahChronology;
import java.time.chrono.HijrahDate;
import java.time.chrono.IsoChronology;

/**
* Created by Mohamed on 3/23/2014
*/
public class Main {
    public static void main(String[] args) {
        HijrahDate date = HijrahChronology.INSTANCE.dateNow();
        System.out.println(date);
        System.out.println(IsoChronology.INSTANCE.date(date));
    }
}

Running this will print on the console the following:


Hijrah-umalqura AH 1435-05-27
2014-03-28

How about creating arbitrary Hijri dates let’s say my 10/11/1404 and printing the full month name? as simple as:

  HijrahDate date = HijrahChronology.INSTANCE.date(1404,11,10);
  System.out.println(date.format(DateTimeFormatter.ofPattern("dd-MMMM-yyyy")));

which will show the output:

  10-Dhuʻl-Qiʻdah-1404

All of this of course is just scratching the surface of what’s possible with the api. For more check the date and time api documentation here http://docs.oracle.com/javase/tutorial/datetime/index.html

Advertisements