【发布时间】:2012-07-18 00:21:03
【问题描述】:
在我的 Android 应用上,我只想在自上次导入至少 X 小时后重新导入我的数据。
我将 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;
}
}
【问题讨论】:
-
你可以只以毫秒为单位存储时间。然后你只需要计算差异并将其除以每小时的毫秒数