2015-10-18 36 views
0

我想使用GregorianCalendar而不是Time類,android文檔如何顯示,但如何管理julian日期作爲int在我的sqlLite數據庫中存儲日期?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); 
} 

而當我查詢一行怎麼能得到正常的日期由朱連日?

回答

1

如果您只需將日期轉換爲保存在數據庫中的整數,則可以使用SimpleDateFormatter將Date轉換爲String,然後將該字符串轉換爲int。像這樣:

Date date = new Date(startDate); 
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); 
String dateString = formatter.format(date); 
int dateInt = Integer.parseInt(dateString); 

這將創建一個整數,您可以輕鬆地保存和檢索數據庫中。

您應該使用類似代碼:

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

謝謝安德烈但我需要的是不考慮時區 –

+0

@FedericoPonti我已經更新了我的答案的解決方案,它現在應該做你所需要的。 –

+0

非常感謝Andrea! –