如何比较android中的两个时间戳?

and*_*dev 6 android timestamp

我想比较两个时间戳,如果它的差异是( - + 5minuts),那么我想显示警告对话框.

即如果目前在我们的手表下午4点,第二次是下午4点45分或下午3点45分,那么警报将显示其他情况.

任何人都可以建议我如何得到这个解决方案.??

我在搜索后发现了获取timeStamp的功能以及如何比较两个时间戳,但对于这种类型的条件是否有任何方法或功能?

谢谢.

我的代码是: -

date= new Date();
currentTime = date.getTime();
if(currentTime !=0 && previousTime !=0){
   String result = (String) DateUtils.getRelativeTimeSpanString(currentTime, previousTime, 0);
 }
Run Code Online (Sandbox Code Playgroud)

我将当前时间保存到上一次lilke tis方式: -

if(currentTime != previousTime){
    previousTime = currentTime;
}
Run Code Online (Sandbox Code Playgroud)

sky*_*ift 10

您可以采取两种方法,具体取决于您是否只想测量经过的时间,或者想要设置未来的时间来进行比较.

第一个类似于Sourabh Saldi的回答,记录了结果

long prevEventTime = System.currentTimeMillis();
Run Code Online (Sandbox Code Playgroud)

然后将它与System.currentTimeMillis()进行比较,直到差异超过300000

正如您所提到的,自1970年1月1日起,服务器的时间戳以毫秒为单位.这意味着它可以直接与System.currentTimeMillis()进行比较.因此,使用:

long serverTimeStamp=//whatever your server timestamp is, however you are getting it.
//You may have to use Long.parseLong(serverTimestampString) to convert it from a string

//3000(millliseconds in a second)*60(seconds in a minute)*5(number of minutes)=300000
if (Math.abs(serverTimeStamp-System.currentTimeMillis())>300000){ 
    //server timestamp is within 5 minutes of current system time
} else {
    //server is not within 5 minutes of current system time
}
Run Code Online (Sandbox Code Playgroud)

另一种方法看起来更接近你正在做的事情 - 使用Date类来存储当前和比较的时间.要使用它们,您需要使用GregorianCalendar类来处理它们.调用

calendar=new GregorianCalendar();
Run Code Online (Sandbox Code Playgroud)

将创建一个新日历,并自动将其日期设置为当前系统时间.您还可以使用GregorianCalendar类中提供的所有函数,使用表单中的某些内容向前或向后滚动时间

calendar.add(GregorianCalendar.MINUTE, 5);
Run Code Online (Sandbox Code Playgroud)

或者将其设置为Date对象的时间

calendar.setTime(date);
Run Code Online (Sandbox Code Playgroud)

在您的情况下,根据您希望的灵活性,GregorianCalendar类和Date类都有after()方法,因此您可能需要以下内容:

在某处创建:

Date currentDate=newDate();
Run Code Online (Sandbox Code Playgroud)

然后设置您的警报点:

calendar=new GregorianCalendar(); //this initialises to the current system time
calendar.setTimeInMillis(<server timestamp>); //change to whatever the long timestamp value from your server is
calendar.add(GregorianCalendar.MINUTE, 5); //set a time 5 minutes after the timestamp
Date beforeThisDate = calendar.getTime();
calendar.add(GregorianCalendar.MINUTE, -10); //set a time 5 minutes before the timestamp
Date afterThisDate = calendar.getTime();
Run Code Online (Sandbox Code Playgroud)

然后检查当前时间是否超过设定的报警点

currentDate.setTime(System.currentTimeMillis());
if ((currentDate.before(beforeThisDate))&&(currentDate.after(afterThisDate))){
    //do stuff, current time is within the two dates (5 mins either side of the server timestamp)
} else {
    //current time is not within the two dates
}
Run Code Online (Sandbox Code Playgroud)

这种方法似乎有点长,但你会发现它非常强大和灵活,可以很容易地扩展到将来设置警报点,或使用GregorianCalendar方法轻松设置日期小时,天或周走向未来.


bid*_*h.r 5

怎么样:

private static final long FIVE_MINUTES = 1000 * 60 * 5; //5 minutes in milliseconds

long currentTime = new Date().getTime();
long previousTime = mPreviousTime;
long differ = (currentTime - previousTime);

if (differ < FIVE_MINUTES && differ > -FIVE_MINUTES ){
  // under +/-5 minutes, do the work
}else{
  // over 5 minutes
}
Run Code Online (Sandbox Code Playgroud)