I want to convert from Gregorian to Hijri(Islamic) date and I need a java class for this converting. I want to give it an Gregorian date in format of "yyyy/mm/dd" as string and it give me the Hijri date in the same format. can anyone help me?
-
1Have you tried something? Have you faced sny particular problem? – araknoid Mar 31 '13 at 11:06
-
I tried some codes found by searching but non of them didn't work probably. – anony Apr 04 '13 at 08:03
7 Answers
Firstly, separate out the conversion part from the formatting/parsing part. You can deal with those easily later - and there are lots of questions on Stack Overflow about that.
Personally I'd use Joda Time, which typically makes life much simpler. For example:
import org.joda.time.Chronology;
import org.joda.time.LocalDate;
import org.joda.time.chrono.IslamicChronology;
import org.joda.time.chrono.ISOChronology;
public class Test {
public static void main(String[] args) throws Exception {
Chronology iso = ISOChronology.getInstanceUTC();
Chronology hijri = IslamicChronology.getInstanceUTC();
LocalDate todayIso = new LocalDate(2013, 3, 31, iso);
LocalDate todayHijri = new LocalDate(todayIso.toDateTimeAtStartOfDay(),
hijri);
System.out.println(todayHijri); // 1434-05-19
}
}
(It feels like there should be a cleaner way of converting dates between chronologies, but I couldn't find one immediately.)

- 2,270
- 1
- 20
- 58

- 1,421,763
- 867
- 9,128
- 9,194
-
+1 I like your way better, short code. On the one I posted is just the way how it works – knowbody Mar 31 '13 at 11:30
-
3+knowbody I don't know why but this code bring me 1434-05-18 not 1434-05-19? how can I fix this problem? – anony Apr 04 '13 at 11:09
-
1@anony: There are two different possiblities here - one is that you're expecting the wrong epoch (there are astronomical and civil epochs) and the other is that you need to use an IslamicChronology with a different leap year pattern. Look at the docs for IslamicChronology for details. – Jon Skeet Apr 04 '13 at 11:13
-
@Jon Skeet, how to convert from hijri to georgian ? and can we set time to LocalDate object ? – Mahmoud Saleh Nov 07 '13 at 11:14
-
@MahmoudSaleh: A `LocalDate` doesn't have a time - it's just a date. You can add a `LocalTime` to it to get a `LocalDateTime` though. Not sure the best way to convert in Joda Time - probably use the `LocalDate(Object, Chronology)` constructor. – Jon Skeet Nov 07 '13 at 11:20
-
1@anony Chronology hijri = IslamicChronology.getInstance(tzSAUDI_ARABIA, IslamicChronology.LEAP_YEAR_15_BASED).getInstanceUTC(); – Ossama May 26 '18 at 06:33
-
@JonSkeet: I have used your above code but the output is one day back **Program o/p Hijri date today - 1439-10-23, Google shows Hijri date today - 24 / 10 / 1439.** I am working on Saudi Arab bank project so I need to set Gr to Hijri – Onic Team Jul 08 '18 at 08:00
-
@Ossama: What is the **tzSAUDI_ARABIA** I am getting error ***tzSAUDI_ARABIA cannot be resolved to a variable*** – Onic Team Jul 08 '18 at 08:06
-
1@VedPrakash: There are multiple variants of the Islamic calendar, with different leap year patterns etc. You need to find out *exactly* which calendar system they want to use. – Jon Skeet Jul 08 '18 at 13:43
-
Also, check [this answer](https://stackoverflow.com/a/63448676/10819573) which is based purely on Java-8 date-time API. – Arvind Kumar Avinash Aug 17 '20 at 13:18
Java 8 is built-in supporting Hejrah Date
example:
import java.time.*;
import java.time.chrono.HijrahChronology;
Date date = new Date(); // Gregorian date
Calendar cl=Calendar.getInstance();
cl.setTime(date);
HijrahDate islamyDate = HijrahChronology.INSTANCE.date(LocalDate.of(cl.get(Calendar.YEAR),cl.get(Calendar.MONTH)+1, cl.get(Calendar.DATE)));
//OUTPUT: Hijrah-umalqura AH 1436-02-03

- 87,526
- 38
- 249
- 254
-
3But not every islamic country follows the official calendar of Saudi-Arabia (the variant supported in Java-8), so I would say, it is NOT enough. Even local authorities inside of Saudi-Arabia have their own deviating customs. – Meno Hochschild Sep 21 '16 at 17:05
-
@AbdennourTOUMI: I'm using Java 6 (How can I get Arabia Standard Time Zone AST) `Chronology hijri = IslamicChronology .getInstance(tzSAUDI_ARABIA, IslamicChronology.LEAP_YEAR_15_BASED).getInstanceUTC();` – Onic Team Jul 08 '18 at 08:51
-
1Also, check [this answer](https://stackoverflow.com/a/63448676/10819573) which is based purely on Java-8 date-time API. – Arvind Kumar Avinash Aug 17 '20 at 13:18
-
This solution is available only for android version => {android.os.Build.VERSION_CODES.O} – صلي علي محمد - Atef Farouk Oct 09 '22 at 03:49
just use Google for example here copied from the link given:
import java.util.Calendar;
/**
* Gregorian-Hijri Dates Converter
*
*
* This Code is used to convert Gregorian dates to Hijri Dates
*
*
*/
public class DateHigri {
static double gmod(double n,double m) {
return ((n % m) + m) % m;
}
static double[] kuwaiticalendar(boolean adjust) {
Calendar today = Calendar.getInstance();
int adj=0;
if(adjust){
adj=0;
}else{
adj=1;
}
if (adjust) {
int adjustmili = 1000 * 60 * 60 * 24 * adj;
long todaymili = today.getTimeInMillis() + adjustmili;
today.setTimeInMillis(todaymili);
}
double day = today.get(Calendar.DAY_OF_MONTH);
double month = today.get(Calendar.MONTH);
double year = today.get(Calendar.YEAR);
double m = month + 1;
double y = year;
if (m < 3) {
y -= 1;
m += 12;
}
double a = Math.floor(y / 100.);
double b = 2 - a + Math.floor(a / 4.);
if (y < 1583)
b = 0;
if (y == 1582) {
if (m > 10)
b = -10;
if (m == 10) {
b = 0;
if (day > 4)
b = -10;
}
}
double jd = Math.floor(365.25 * (y + 4716)) + Math.floor(30.6001 * (m + 1)) + day
+ b - 1524;
b = 0;
if (jd > 2299160) {
a = Math.floor((jd - 1867216.25) / 36524.25);
b = 1 + a - Math.floor(a / 4.);
}
double bb = jd + b + 1524;
double cc = Math.floor((bb - 122.1) / 365.25);
double dd = Math.floor(365.25 * cc);
double ee = Math.floor((bb - dd) / 30.6001);
day = (bb - dd) - Math.floor(30.6001 * ee);
month = ee - 1;
if (ee > 13) {
cc += 1;
month = ee - 13;
}
year = cc - 4716;
double wd = gmod(jd + 1, 7) + 1;
double iyear = 10631. / 30.;
double epochastro = 1948084;
double epochcivil = 1948085;
double shift1 = 8.01 / 60.;
double z = jd - epochastro;
double cyc = Math.floor(z / 10631.);
z = z - 10631 * cyc;
double j = Math.floor((z - shift1) / iyear);
double iy = 30 * cyc + j;
z = z - Math.floor(j * iyear + shift1);
double im = Math.floor((z + 28.5001) / 29.5);
if (im == 13)
im = 12;
double id = z - Math.floor(29.5001 * im - 29);
double[] myRes = new double[8];
myRes[0] = day; // calculated day (CE)
myRes[1] = month - 1; // calculated month (CE)
myRes[2] = year; // calculated year (CE)
myRes[3] = jd - 1; // julian day number
myRes[4] = wd - 1; // weekday number
myRes[5] = id; // islamic date
myRes[6] = im - 1; // islamic month
myRes[7] = iy; // islamic year
return myRes;
}
static String writeIslamicDate() {
String[] wdNames = {"Ahad", "Ithnin", "Thulatha", "Arbaa", "Khams",
"Jumuah", "Sabt"};
String[] iMonthNames = {"Muharram", "Safar", "Rabi'ul Awwal",
"Rabi'ul Akhir", "Jumadal Ula", "Jumadal Akhira", "Rajab",
"Sha'ban", "Ramadan", "Shawwal", "Dhul Qa'ada", "Dhul Hijja"};
// This Value is used to give the correct day +- 1 day
boolean dayTest=true;
double[] iDate = kuwaiticalendar(dayTest);
String outputIslamicDate = wdNames[(int) iDate[4]] + ", " + iDate[5] + " "
+ iMonthNames[(int) iDate[6]] + " " + iDate[7] + " AH";
return outputIslamicDate;
}
}

- 8,106
- 6
- 45
- 70
-
5I think I'd rather use an existing library than have to include all of that code in my own project :) – Jon Skeet Mar 31 '13 at 11:28
-
it don't work right.it bring similar dates for some days. for ex. I have changed it's input to get string and it made same date for "2014/1/29" and "2014/2/1". however I prefer to use a library than all this code. thank you for your help ;) – anony Apr 04 '13 at 08:10
-
-
-
Also, check [this answer](https://stackoverflow.com/a/63448676/10819573) which is based purely on Java-8 date-time API. – Arvind Kumar Avinash Aug 17 '20 at 13:19
Try ummalqura-calendar which implements java.util.Calendar
.
Using this calendar you can convert from Umm Al-Qura to Gregorian and vice versa,
and also you can use java.text.SimpleDateFormat
to format dates.

- 316
- 3
- 6
-
-
Also, check [this answer](https://stackoverflow.com/a/63448676/10819573) which is based purely on Java-8 date-time API. – Arvind Kumar Avinash Aug 17 '20 at 13:18
sample way to convert date on android platform import
import org.joda.time.Chronology;
import org.joda.time.DateTime;
import org.joda.time.chrono.ISOChronology;
import org.joda.time.chrono.IslamicChronology;
import org.joda.time.LocalDate;
then
implements HijriDatePickerDialog.OnDateSetListener
on onDateSet method
@Override
public void onDateSet(HijriDatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
Chronology iso = ISOChronology.getInstanceUTC();
Chronology hijri = IslamicChronology.getInstanceUTC();
DateTime dtHijri = new DateTime(year,monthOfYear,monthOfYear,dayOfMonth,dayOfMonth,hijri);
DateTime dtIso = new DateTime(dtHijri, iso);
Log.i("converted date" ,String.valueOf(dtIso));
}

- 549
- 6
- 13
-
1Also, check [this answer](https://stackoverflow.com/a/63448676/10819573) which is based purely on Java-8 date-time API. – Arvind Kumar Avinash Aug 17 '20 at 13:21
For android API 26 and above you can use
import java.time.chrono.HijrahDate
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
HijrahDate.now()
}
It is a build-in class in Java.

- 1,290
- 10
- 20
Assuming hijri date in format yyyy/mm/dd & gregorian date in format yyyy-mm-dd,
you can adapt this code to convert from one to another
import java.time.*;
import java.time.chrono.*;
import java.time.format.DateTimeFormatter;
public class DateUtils {
private DateUtils() {
}
public static String hijriToGreogorian(String hijriDateStr) {
final String[] hijriDateArr = hijriDateStr.split("/");
final HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(HijrahEra.AH,
Integer.parseInt(hijriDateArr[0]), Integer.parseInt(hijriDateArr[1]), Integer.parseInt(hijriDateArr[2]));
return IsoChronology.INSTANCE.date(hijrahDate).toString();
}
public static String greogorianToHijri(String greoDateStr) {
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return HijrahChronology.INSTANCE.date(LocalDate.parse(greoDateStr, formatter))
.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
}
public static void main(String[] args) {
System.out.println(greogorianToHijri("2023-09-15")); // 1445/02/30
System.out.println(hijriToGreogorian("1445/02/30")); // 2023-09-15
}
}

- 136
- 5