检查当前时间比从一个多小时" LAST_UPDATED"时间 [英] Check if current time is more than an hour from "last_updated" time
问题描述
在我的Android应用程序,我想只能重新导入我的数据如果自上次导入已经至少X小时。
我存储LAST_UPDATED时间在我的SQLite数据库格式为: 2012/07/18 00:01:40
如何才能从此小时或者类似的东西?
我的code迄今:
包com.sltrib.utilities;进口java.text.SimpleDateFormat的;
进口的java.util.Calendar;公共类DateHelper
{ 现在公共静态字符串()
{
日历的currentdate = Calendar.getInstance();
SimpleDateFormat的格式化=新的SimpleDateFormat(YYYY / MM / DD HH:MM:SS);
串dateNow = formatter.format(currentDate.getTime());
//System.out.println(\"Now日期是:=>中+ dateNow);
返回dateNow;
} 公共静态INT hoursAgo(字符串日期时间)
{
//返回的时间它已经因为给定的时间数
// INT小时=?
//返回小时;
}}
您打算要两个日历
秒之间做数学或日期
秒。
注意:面面观日期
是pcated,见下文德$ P $日历
!
下面是一个使用日期
一个例子:
公共静态INT hoursAgo(字符串日期时间){
日期日期=新的SimpleDateFormat(YYYY / MM / DD HH:MM:SS,Locale.ENGLISH).parse(日期时间); //解析成Date对象
现在日期= Calendar.getInstance()的getTime()。 //现在获取时间
长differenceInMillis = now.getTime() - date.getTime();
长differenceInHours =(differenceInMillis)/ 1000L / 60L / 60L; //通过米利斯/秒的分割,秒/分钟,分钟/小时
返回(INT)differenceInHours;
}
这里有一些涉及的try / catch
块(你或许应该用处理抛出
),但这的基本思想。
修改:由于日期
是德precated的部分地区,这里是一个使用日历<同样的方法/ code>:
公共静态INT hoursAgo(字符串日期时间){
日历日期= Calendar.getInstance();
date.setTime(新的SimpleDateFormat(YYYY / MM / DD HH:MM:SS,Locale.ENGLISH).parse(日期时间)); //解析成Date对象
现在日历= Calendar.getInstance(); //现在获取时间
长differenceInMillis = now.getTimeInMillis() - date.getTimeInMillis();
长differenceInHours =(differenceInMillis)/ 1000L / 60L / 60L; //通过米利斯/秒的分割,秒/分钟,分钟/小时
返回(INT)differenceInHours;
}
On my Android App, I'd like to only re-import my data if it's been at least X hours since the last import.
I'm storing the last_updated time in my sqlite database in this format: 2012/07/18 00:01:40
How can I get "hours from then" or something like that?
My code thus far:
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;
}
}
You're going to want to do math between two Calendar
s or Date
s.
Note: Aspects of Date
are deprecated, see below for Calendar
!
Here's an example using Date
:
public static int hoursAgo(String datetime) {
Date date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH).parse(datetime); // Parse into Date object
Date now = Calendar.getInstance().getTime(); // Get time now
long differenceInMillis = now.getTime() - date.getTime();
long differenceInHours = (differenceInMillis) / 1000L / 60L / 60L; // Divide by millis/sec, secs/min, mins/hr
return (int)differenceInHours;
}
There are some try/catch
blocks involved here (which you should probably handle with throws
), but this is the basic idea.
Edit: Since parts of Date
are deprecated, here is the same method using Calendar
:
public static int hoursAgo(String datetime) {
Calendar date = Calendar.getInstance();
date.setTime(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH).parse(datetime)); // Parse into Date object
Calendar now = Calendar.getInstance(); // Get time now
long differenceInMillis = now.getTimeInMillis() - date.getTimeInMillis();
long differenceInHours = (differenceInMillis) / 1000L / 60L / 60L; // Divide by millis/sec, secs/min, mins/hr
return (int)differenceInHours;
}
这篇关于检查当前时间比从一个多小时&QUOT; LAST_UPDATED&QUOT;时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!