2016-03-08 44 views
0

我試圖更新我的舊代碼,但更新Time.getJulianDay時出現了一些問題。我試圖使用GregorianCalendar,但我無法將其轉換爲int。 ¿有什麼辦法可以做到嗎?替代Time.julianDay的問題

這是我的代碼:

public void addDate(ExtendedCalendarView calendarView,String eventname,String description, String location) 
{ 
    try { 
     if(startYear==-1) 
      throw new Exception("No has indicado los valores de las horas"); 

     cal.set(startYear, startMonth - 1, startDay, startHour, startMinute); 
     long time = cal.getTimeInMillis(); 

     ContentValues values = new ContentValues(); 
     values.put(CalendarProvider.COLOR, Event.COLOR_RED); 
     values.put(CalendarProvider.DESCRIPTION, description); 
     values.put(CalendarProvider.LOCATION, location); 
     values.put(CalendarProvider.EVENT, eventname); 
     values.put(CalendarProvider.START, time); 
     values.put(CalendarProvider.START_DAY, getJulianDay(time)); 

     cal.set(stopYear, stopMonth - 1, stopDay, stopHour, stopMinute); 
     time = cal.getTimeInMillis(); 
     values.put(CalendarProvider.END, time); 
     values.put(CalendarProvider.END_DAY, getJulianDay(time)); 

     context.getContentResolver().insert(CalendarProvider.CONTENT_URI, values); 

     restartTime(); 
     calendarView.refreshCalendar(); 

    } 
    catch (Exception ex) 
    { 
     Log.d("asd", "ERROR;"+ex.getMessage()); 
    } 

} 

private int getJulianDay(long time) 
{ 
    GregorianCalendar date=(GregorianCalendar) GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC")); 
    date.setTime(new Date(time)); 
    date.set(Calendar.HOUR_OF_DAY, 0); 
    date.set(Calendar.MINUTE,0); 
    date.set(Calendar.SECOND, 0); 
    date.set(Calendar.MILLISECOND,0); 


    //return Time.getJulianDay(time, TimeUnit.MILLISECONDS.toSeconds(timeZone.getOffset(time))); 
} 

感謝

回答

0

你有沒有考慮使用JodaTime庫在你的應用程序?它將使Android上的日期/時間更容易工作。輔助函數toJulianDay()和JulianDayNumber()對於你正在做的事情來說是完美的。這些方法分別返回double和long,如果絕對需要,你可以將結果轉換或轉換爲int(但你將失去一些精度)。

toJulianDay(long)方法根據天數從中午開始計算天文學儒略日。此方法計算日期從午夜開始的變體。 JDN 0用於相當於公元前4713年1月1日(朱利安)的日期。因此,這些日子在小朱利安日之前12個小時開始。

Here is the full documentation for more details.link to the JodaTime project

+0

它的作品謝謝! –