Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two timestamps in android?

I want to compare two timestamps and if the difference of that is (-+5minuts) then I want to display alert dialog.

i.e. If currently in our watch 4PM the second time is 4.05PM or 3.55PM then alert will display else not.

Can anyone suggest me the way how can I get the solution of this.??

I found after search the function of getting timeStamp and how to compare two timestamps but for this type of condition is there any method or function?

Thanks.

My code is:-

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

And I am storeing current time in to previous time lilke tis way :-

if(currentTime != previousTime){
    previousTime = currentTime;
}
like image 928
anddev Avatar asked Aug 28 '12 09:08

anddev


2 Answers

There's two approaches you could take, depending on whether you just want to measure time elapsed, or want to set future times to compare to.

The first is similar to Sourabh Saldi's answer, record the result from

long prevEventTime = System.currentTimeMillis();

then compare it with System.currentTimeMillis() until the difference is more than 300000

As you have mentioned, your timestamp from the server is in milliseconds since January the 1st, 1970. This means it is directly comparable to System.currentTimeMillis(). As such, use:

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
}

The other method looks closer to what you're already doing - using the Date class to store the current and compared time. To use these, you'll want to be using the GregorianCalendar class to handle them. Calling

calendar=new GregorianCalendar();

will create a new calendar, and automatically set it's date to the current system time. You can also use all the functions supplied in the GregorianCalendar class to roll the time forward or backward using something of the form

calendar.add(GregorianCalendar.MINUTE, 5);

or set it to a Date object's time with

calendar.setTime(date);

In your case, depending on how much flexibility you want both the GregorianCalendar class and the Date class have after() methods, so you probably want something like the following:

Create somewhere:

Date currentDate=newDate();

Then set your alarm point:

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();

Then check if the current time is past the set alarm point with

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
}

This approach can seem a bit more long winded, but you'll find it is very robust and flexible, and can easily be extended to set alarm points far in the future, or use the GregorianCalendar methods to easily set dates hours, days or weeks into the future.

like image 100
skyrift Avatar answered Oct 21 '22 03:10

skyrift


How about just:

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
}
like image 37
biddulph.r Avatar answered Oct 21 '22 03:10

biddulph.r