2016-09-30 44 views
0

由於時間類已被棄用,我不能再在以下函數中使用getJulianDay()。我想使用Calender或GregorianCalender類來實現相同的功能。我部分地從[question]得到了我的答案:Time getJulianDay() deprecated which alternatives to convert date to julian and vice versa? 但我是新來的這樣的類,所以我不知道如何將GregorianCalender轉換爲long,以便我可以在函數中返回它。我如何獲得Julian日期?謝謝!Time.getJulianDay()已棄用。哪些代碼會完成工作?

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); 
} 
+0

有[許多問題與解答SO](/搜索?Q =%5Bjava%5D +朱利安+日期)朱利安日期。他們怎麼樣沒有幫助?例如,[這一個](http://stackoverflow.com/questions/20035403/julian-date-to-regular-date-conversion)? –

回答

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