anddev
anddev

Reputation: 3204

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;
}

Upvotes: 6

Views: 19717

Answers (5)

biddulph.r
biddulph.r

Reputation: 5266

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
}

Upvotes: 5

skyrift
skyrift

Reputation: 743

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.

Upvotes: 13

raman
raman

Reputation: 337

Use this following method to change your dates in epoch format

public Long getChnagedDate(Activity act,String date) throws ParseException
{
      long epoch = new java.text.SimpleDateFormat ("MM/dd/yyyy HH:mm:ss aa").parse(date).getTime();
      return epoch/1000;
}

and after check the difference in http://www.epochconverter.com. Hope it helps you.

Upvotes: 0

Sourabh Saldi
Sourabh Saldi

Reputation: 3587

 long etime = 0;
final long time1 = uptimeMillis();
/* do something */
final long time2 = uptimeMillis();
if (time2 < time1) {
    etime = Long.MAX_VALUE - time1 + time2;
} else {
    etime = time2 - time1;
}

then check this etime and do as required!!1

Upvotes: 1

Yahor10
Yahor10

Reputation: 2132

Joda time will help you with this task.

import org.joda.time.Interval;

http://joda-time.sourceforge.net/

Upvotes: -1

Related Questions