1

I want to convert Gregorian date to Hijri date in java 8 using HijrahChronology library. I've tried the codes below but don't work accurately.

  Date date = new Date(); // Gregorian date
  Calendar cl=Calendar.getInstance();
  cl.set(Calendar.YEAR, date.getYear());

  HijrahDate islamyDate = HijrahChronology.INSTANCE.date(LocalDate.of(cl.get(Calendar.YEAR),cl.get(Calendar.MONTH)+1, cl.get(Calendar.DATE)));
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("uuuu");
        String gregorianString = "1994";
        LocalDate gregorianDate = LocalDate.parse(gregorianString, dateFormatter);
        HijrahDate islamicDate = HijrahDate.from(gregorianDate);
        System.out.println("Islamic date: " + islamicDate);

Update: I want to convert only the year part.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
user2340345
  • 793
  • 4
  • 16
  • 38
  • 1
    Does this answer your question? [converting gregorian to hijri date](https://stackoverflow.com/questions/15728744/converting-gregorian-to-hijri-date) – mohammedkhan Aug 17 '20 at 09:36
  • @mohammedkhan - None of the answers in your link uses the modern `java.time` API. Either they are based on Joda Time API or outdated and error-prone legacy `java.util` API. – Arvind Kumar Avinash Aug 17 '20 at 11:13
  • @ArvindKumarAvinash the second answer on there does: https://stackoverflow.com/a/27120886/3415090 – mohammedkhan Aug 17 '20 at 12:40
  • @mohammedkhan - As I mentioned earlier, the answer which you are pointing to is mixing the error-prone legacy `java.util` API with the smart modern `java.time` API. One should stop using the poorly designed `java.util` API completely. – Arvind Kumar Avinash Aug 17 '20 at 12:44
  • @ArvindKumarAvinash agreed the answer isn't perfect. Your comment sounded like none of the answers used `java.time.*` which wasn't entirely accurate, that one does, but as you say it unnecessarily mixes with `java.util.Calendar`. Given that question is the same as this one, and is older, I would recommend you add your answer below to that question too. – mohammedkhan Aug 17 '20 at 12:47

1 Answers1

4

You can simply pass a TemporalAccessor e.g. LocalDate, ZonedDateTime etc. to HijrahChronology.INSTANCE#date as shown below:

import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.time.chrono.HijrahChronology;
import java.time.chrono.HijrahDate;
import java.time.temporal.TemporalAdjusters;

public class Main {
    public static void main(String[] args) {
        // Change the time-zone as per your requirement.
        ZoneId myZoneId = ZoneId.of("Etc/UTC");

        HijrahDate hijrahDate1 = HijrahChronology.INSTANCE.date(LocalDate.now(myZoneId));
        System.out.println(hijrahDate1);

        // ########Another example########

        // HijrahDate on the last day of January 1994 at the time zone of Etc/UTC
        HijrahDate hijrahDate2 = HijrahChronology
                .INSTANCE                                   // Instance of HijrahChronology
                .date(LocalDate.of(1994, Month.JANUARY, 1)  // LocalDate of 01-Jan-1994
                .with(TemporalAdjusters.lastDayOfMonth())   // On the last day of Jan-1994
                .atStartOfDay()                             // At the start of the day i.e. 00:00
                .atZone(myZoneId));                         // At the time zone of Etc/UTC
        System.out.println(hijrahDate2);
    }
}

Output:

Hijrah-umalqura AH 1441-12-27
Hijrah-umalqura AH 1414-08-19

Learn more about the Java-8 date-time API at Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110