不推荐使用Time.getJulianDay()。相反,哪些代码可以完成工作?

时间:2016-09-30 09:21:29

标签: java android deprecated

由于不推荐使用Time类,我不能再使用以下函数中的getJulianDay()了。我想用Calender或GregorianCalender类来实现同样的目的。我部分从[问题]得到答案:Time getJulianDay() deprecated which alternatives to convert date to julian and vice versa? 但我是这些类的新手,所以我不知道如何将GregorianCalender转换为long,以便我可以在函数中返回它。我如何获得朱利安日期?谢谢!

public static long normalizeDate(long startDate) {
    // normalize the start date to the beginning of the (UTC) day
    Time time = new Time();
    time.set(startDate);
    int julianDay = Time.getJulianDay(startDate, time.gmtoff);
    return time.setJulianDay(julianDay);
}

1 个答案:

答案 0 :(得分:0)

这是否适合您的需要?

public static long normalizeDate(long startDate) {
    // normalize the start date to the beginning of the (UTC) day
    GregorianCalendar date = (GregorianCalendar)     GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"));
    date.setTime(new Date(startDate));
    date.set(Calendar.HOUR_OF_DAY, 0);
    date.set(Calendar.MINUTE, 0);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND, 0);

    //transform your calendar to a long in the way you prefer
    return date.getTimeInMillis();
}