在 android 中获取公历的当前儒略日数

Get current julian day number with gregorian calendar in android

android.text.format.Time has a method called getJulianDay 即 returns 自纪元以来的天数。但是文档说:

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

GregorianCalendar 中是否有任何方法具有相同的作用?

AFIK 没有,但也许这会有所帮助,小数部分将被忽略。

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

计算公式来自 https://en.wikipedia.org/wiki/Julian_day 参见“将公历日期转换为儒略日数字”部分

在 Kotlin 中,当前日期或 1970 年之后:

/**
 * 1970-01-01T00:00     Astronomical Julian Day= 2_440_587.5
 * @param fechaZulu
 * @return Astronomical Julian Day
 */
fun DJdiasJulianosAstromicos(fechaZulu: Calendar): Double {
    return 2_440_587.5 + fechaZulu.timeInMillis / 86_400_000.0 //=(1000 * 60 * 60 * 24.0)
}