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.
java.time
API. Either they are based on Joda Time API or outdated and error-prone legacyjava.util
API. – Revitalizejava.util
API with the smart modernjava.time
API. One should stop using the poorly designedjava.util
API completely. – Revitalizejava.time.*
which wasn't entirely accurate, that one does, but as you say it unnecessarily mixes withjava.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. – Existential