在我的Android應用程序中,如果自上次導入至少X小時後才重新導入數據,檢查當前時間距「last_updated」時間是否超過一個小時
我存儲的LAST_UPDATED時間在我的SQLite數據庫格式爲:2012/07/18 00:01:40
我怎樣才能得到「從此小時」或類似的東西?
我的代碼迄今:
package com.sltrib.utilities;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DateHelper
{
public static String now()
{
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String dateNow = formatter.format(currentDate.getTime());
//System.out.println("Now the date is :=> " + dateNow);
return dateNow;
}
public static int hoursAgo(String datetime)
{
//return the number of hours it's been since the given time
//int hours = ??
//return hours;
}
}
你可以只保存在毫秒的時間。然後,你只需要計算差異,並以每小時毫秒數爲單位來劃分它 – Weeman 2012-07-18 00:32:52