javaandroidtimecalendargregorian-calendar

Get current julian day number with gregorian calendar in android


android.text.format.Time has a method called getJulianDay that returns the day number since epoch. But docs says that:

This class was deprecated in API level 22. Use GregorianCalendar instead.

Is there any method in GregorianCalendar that does the same work?


Solution

  • AFIK there is not, but maybe this can help, fractional parts are ignored.

    public static int getJulianDay(Calendar cal) {
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH)+1; //Note January returns 0
        int date = cal.get(Calendar.DATE);
        return (1461 * (year + 4800 + (month - 14) / 12)) / 4 
                + (367 * (month - 2 - 12 * ((month - 14) / 12))) / 12
                - (3 * ((year + 4900 + (month - 14) / 12) / 100)) / 4 + date - 32075;
    }
    

    The formula for the calculation is from https://en.wikipedia.org/wiki/Julian_day see section "Converting Gregorian calendar date to Julian Day Number"