3

I use a PersianDatePicker to select a date. This works fine, but I want to convert selected Persian date to a Gregorian date.

I have found some code and example from internet but the calculation is not right. When I select current date in Persian 5.7.1396 and convert this date to Gregorian I get 4.8.2017.

public static int g_days_in_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30,
        31, 30, 31 };
public static int j_days_in_month[] = { 31, 31, 31, 31, 31, 31, 30, 30, 30,
        30, 30, 29 };
/**
 * Converts Persian date to gregorian date
 * */
public static Calendar getGregorianCalendar(int year, int month, int day) {
    int gy, gm, gd;
    int jy, jm, jd;
    long g_day_no, j_day_no;
    boolean leap;
    int i;
    jy = year - 979;
    jm = month - 1;
    jd = day - 1;
    j_day_no = 365 * jy + (jy / 33) * 8 + (jy % 33 + 3) / 4;
    for (i = 0; i < jm; ++i)
        j_day_no += j_days_in_month[i];
    j_day_no += jd;
    g_day_no = j_day_no + 79;
    gy = (int) (1600 + 400 * (g_day_no / 146097)); /*
                                                     * 146097 = 365*400 +
                                                     * 400/4 - 400/100 +
                                                     * 400/400
                                                     */
    g_day_no = g_day_no % 146097;
    leap = true;
    if (g_day_no >= 36525) /* 36525 = 365*100 + 100/4 */
    {
        g_day_no--;
        gy += 100 * (g_day_no / 36524); /* 36524 = 365*100 + 100/4 - 100/100 */
        g_day_no = g_day_no % 36524;
        if (g_day_no >= 365)
            g_day_no++;
        else
            leap = false;
    }
    gy += 4 * (g_day_no / 1461); /* 1461 = 365*4 + 4/4 */
    g_day_no %= 1461;
    if (g_day_no >= 366) {
        leap = false;
        g_day_no--;
        gy += g_day_no / 365;
        g_day_no = g_day_no % 365;
    }
    for (i = 0; g_day_no >= g_days_in_month[i]
            + parsBooleanToInt(i == 1 && leap); i++)
        g_day_no -= g_days_in_month[i] + parsBooleanToInt(i == 1 && leap);
    gm = i + 1;
    gd = (int) (g_day_no + 1);
    GregorianCalendar gregorian = new  GregorianCalendar(gy, gm - 1, gd);
    return gregorian;
}
private static int parsBooleanToInt(Boolean sample) {
    if (sample)
        return 1;
    else
        return 0;
}
Assafs
  • 3,257
  • 4
  • 26
  • 39
guguli
  • 249
  • 3
  • 7
  • 16
  • Hard to say where your algorithm seems to be wrong. Reinventing calendar stuff is usually hard. Using my lib Time4A yields what you need: ` System.out.println(PersianCalendar.of(1396, 7, 5).transform(PlainDate.class));` // 2017-09-27 – Meno Hochschild Oct 24 '17 at 11:59

2 Answers2

2

Use PersianCaldroid library as your DatePicker which will bring you the APIs you need to convert dates, as well. Here's how you can convert Persian date to Gregorian date:

CivilDate civilDate = DateConverter.persianToCivil(new PersianDate());
Log.e("Gregorian Date", civilDate.toString());

Gregorian Date: 2018/05/19
Dariush Malek
  • 914
  • 1
  • 6
  • 14
1

I have used this library for convert Jalali date to Gregorian date and vice versa :

https://github.com/samanzamani/PersianDate

You can use the toGregorian(int year, int month, int day) method from it.

Sadegh Ghanbari
  • 1,271
  • 15
  • 29
  • This library has some issues on some phone models for example A30, highly recommend that don't use it – smrf Nov 26 '20 at 06:51
  • Is there a better library ? @seyedrezafar – Sadegh Ghanbari Nov 26 '20 at 10:15
  • use this com.github.dariushm2:PersianCaldroid:1.0.1 instead, it has a DateConverter which do the job well, I'm moving to this one right now, no error yet. @Sadegh Ghanbari – smrf Nov 28 '20 at 07:27