2

I use this code to convert Gregorian date to Hijri :

   import java.util.Calendar;  
    /** 
     * Gregorian-Hijri Dates Converter 
     *  
     *  
     * This Code is used to convert Gregorian dates to Hijri Dates  
     *  
     * 
     */  

    public class Hijri {  

        static double gmod(double n,double  m) {  
            return ((n % m) + m) % m;  
        }  

        static double[] kuwaiticalendar(boolean adjust, int Y, int M, int D) {  
            Calendar theDate = Calendar.getInstance();  
            theDate.set(Y, M, D);
            int adj=0;  
            if(adjust){  
                adj=0;  
            }else{  
                adj=1;  
            }  

            if (adjust) {  
                int adjustmili = 1000 * 60 * 60 * 24 * adj;  
                long thedatemili = theDate.getTimeInMillis() + adjustmili;  
                theDate.setTimeInMillis(thedatemili);  
            }  
            double day = theDate.get(Calendar.DAY_OF_MONTH);  
            double  month = theDate.get(Calendar.MONTH);  
            double  year = theDate.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 = 1948086;
            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 + 29.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(int Y, int M, int D) {  
            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, Y, M, D);  
            String outputIslamicDate = (int) iDate[7] + "-" + (int) iDate[6] + "-" + (int) iDate[5];  

            return outputIslamicDate;  
        }  

    }

when I set this date 2014/7/27 it returns 1435/10/0 what shall I do?

Jens
  • 67,715
  • 15
  • 98
  • 113
  • 1
    What should it return? (Sorry, I don't understand the Hijri calendar) – Squonk Aug 14 '14 at 10:01
  • it should returns 1435/9/30 – user2764027 Aug 14 '14 at 10:03
  • OK, so 1435 is the Hijri year, correct? Is 9 the same as Gregorian calendar for September? And 30 the 30th day of month? I know that the `Calendar` class starts at 0 (for January) which might explain being one more than you expect. – Squonk Aug 14 '14 at 10:12
  • the code has kind of problem and I don't know what is that (I didn't write the code!) – user2764027 Aug 14 '14 at 11:03
  • OK, I'm not sure I can help immediately but I've read up on Hijri and understand that 2014/7/27 should be either 1435/9/29 or 1435/9/30 (depending on if I enter the time as before or after midday in this Calendar Converter site... http://www.fourmilab.ch/documents/calendar/ ). I'll try and look at this problem again later - it seems your result has incremented the month to 10 but also set the day of month to 0 which should never happen in any calendar. – Squonk Aug 14 '14 at 11:53
  • http://stackoverflow.com/questions/15728744/converting-gregorian-to-hijri-date – CommonsWare Aug 15 '14 at 21:50

0 Answers0