0% found this document useful (0 votes)
245 views

Gregorian-Hijri Dates Converter JAVA (Java in General Forum at Coderanch) PDF

Uploaded by

Shoaib Quraishi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
245 views

Gregorian-Hijri Dates Converter JAVA (Java in General Forum at Coderanch) PDF

Uploaded by

Shoaib Quraishi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

A friendly place for programming greenhorns!

Register / Login

Java Forums
Java Java

in General

Gregorian-Hijri Dates Converter JAVA


Post by: Ghaith Shamayleh, Greenhorn
on Apr 13, 2011 05:08:52
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;

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 - 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;
}
}

Post by: Joanne Neal, Rancher


on Apr 13, 2011 06:49:47
Did you have a question or are you just sharing your code ?

Post by: Ghaith Shamayleh, Greenhorn


on Apr 13, 2011 06:57:35

Am just sharing the code

Post by: Jesper de Jong, Saloon Keeper


on Apr 13, 2011 07:00:55
Ok, thanks then.
I do have some comments about the code. It contains a lot of "magic" numbers: 365.25, 4716, 30.6001, 1524, 2299160,
1867216.25, 36524.25, 122.1, 10631, 1948084, 1948085, 8.01, 28.5001, 29.5001, ...
What do all those numbers mean? How do you know this code works correctly? For which range of dates does it work
correctly?

Post by: Ghaith Shamayleh, Greenhorn


on Apr 13, 2011 07:04:02
These numbers are used to calculate the Islamic Higri date, you can check if the date returned on the following site if its
correct
http://www.islamicity.com/PrayerTimes/defaultHijriConv.asp

Post by: Ulf Dittmer, Rancher


on Apr 13, 2011 07:06:06
365.25 in particular sounds suspicious - does this code handle leap years correctly in the long run?
Maybe you can provide a link to Wikipedia page or something like that where the topic (not necessarily the calculations)
are explained.

Post by: Ghaith Shamayleh, Greenhorn

on Apr 13, 2011 07:08:47


http://en.wikipedia.org/wiki/Islamic_calendar
http://en.wikipedia.org/wiki/Kuwaiti_algorithm

Post by: abu alfouz, Ranch Hand


on Apr 13, 2011 07:20:38
Very useful .. thank you

Post by: Rob Spoor, Sheriff


on Apr 14, 2011 02:57:05

ghaith shamayleh wrote:

These numbers are used to calculate the Islamic Higri date, you can check if the date returned on the following site if its correct
http://www.islamicity.com/PrayerTimes/defaultHijriConv.asp

Then make them constants - private static final fields of the right type (int / double) with a descriptive name.

Post by: Campbell Ritchie, Sheriff


on Apr 14, 2011 04:13:49
The duration of a year is not 365.25 days. According to this webpage that is 11min 14 seconds too long. anyway, using
floating-point arithmetic is bound to cause imprecisions.

Post by: Jesper de Jong, Saloon Keeper


on Apr 14, 2011 07:04:25

Campbell Ritchie wrote:

The duration of a year is not 365.25 days. According to this webpage that is 11min 14 seconds too long. anyway, using floatingpoint arithmetic is bound to cause imprecisions.

It's much more complicated than that.


According to Wikipedia, a Julian year is 365.25 days. I guess that the definition on the above website is the length of a
Gregorian year (a year according to the usual Gregorial calendar). But there are also other ways to define a year: for
example a sidereal year, which is the time it takes the Earth to make exactly one orbit around the Sun, is slightly different
(365 days, 6 hours 9 minutes, 9.7676 seconds). And there are other definitions which are again slightly different.

Post by: autobot


Wink, wink, nudge, nudge, say no more ... http://richsoil.com/cards

New Topic

All times above are in ranch (not your local) time.


The current ranch time is
Apr 27, 2016 03:09:37.

Search | Recent Topics | Flagged Topics | Zero Replies


Copyright 1998-2016 Paul Wheaton

Or visit our non-mobile (full) site

You might also like