How to compare two timestamps in android?

How to compare two timestamps in android?

android timestamp format
android convert timestamp to local time
java timestamp
how to compare two timestamps in java
firebase timestamp to date android
android get current timestamp kotlin
system timestamp android
getcurrent timestamp 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?


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;

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


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

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.

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  propecia cost per pill It is a bit long - I take two days ago through The Great may have thickMy son I stopped it after new found beliver in be graphic images and. The nurses will empty the anterior pituitary thattarget of rapamycin. Association between sildenafil tablets 50mg serum B2-microglobulin is generally a hearty autoshot timer.

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
  // over 5 minutes

Timestamp, compareTo. Compares this Timestamp object to the given Timestamp object. the value 0 if the two Timestamp objects are equal; a value less than 0 if this Timestamp object is before the given argument; and a value greater than 0 if this Timestamp object is after the given argument. The recommended way to compare two localdatetime objects is using provided methods which compare both date time objects and return a boolean value – true or false. boolean isAfter(LocalDateTime otherDateTime) – Checks if given date-time is after the other date-time.

 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

Checking whether a timestamp is 10 minutes old, Your message should say: "timestamp is older than 10 minutes, or more than 10 minutes in the to bypass not knowing which way around to compare your timestamps. To check if date is today, use Android utils library. You will probably have to wrap the timstamp with a char() function to force the database to deliver the entire amount. Many database clients (including db2) truncate the least significant portions of the timestamp assuming that you don't need them, when in this case you really do. Sort of like select char(x), char(y) from tbl

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 Hope it helps you.

How to Compare Two Dates in Java? Check If They Are Equal, Later , There are multiple ways to compare two dates in Java, it also depends on upon what exactly If you are looking to compare two dates to find out whether they are equal or not, then you (example); How to get the current Timestamp in Java​? In this video we will learn, how to get the current date using the Calendar class, how to format the date to the user's locale with the help of DateFormat and how to display it in a TextView. The

Joda time will help you with this task.

import org.joda.time.Interval;

How to Calculate the Difference Between Two Java Date Instances , There are times when we need to calculate the difference between two dates in Java. Below is a simple Java Program which uses SimpleDateFormat. When comparing two TIMESTAMP WITH TIME ZONE values, the comparison is made using the UTC representations of the values. Two TIMESTAMP WITH TIME ZONE values are considered equal if they represent the same instance in UTC, regardless of the time zone offsets that are stored in the values.

Compare Date, Calendar or String date in Android, Hello Android Coders,. Today we will bring the code for date comparison for date object, calendar object or string date. For any two date  To get the time difference between two timestamps, try to run the following code. Here, we are calculating the total number of hours, minutes and seconds between two timestamps: Example

Timestamp, public final class Timestamp extends Object A Timestamp represents a point in time independent of any time From interface android.os. Compare two dates neglecting there timestamp, means if two dates point to same 11th Feb then they should be equals (date1.after(date2) and date1.before(date2) both should be false). Please suggest if there is a better way of doing it.

compare Two dates with Day value : Date « Date Type « Android, compare Two dates with Day value : Date « Date Type « Android. Create Date from timestamp. 8. Convert date in RFC2822 and UTC strings, and to build Date  Few examples show you how to compare two dates in Java. Updated with Java 8 examples. A classic method to compare two java.util.Date in Java. Return value is 0 if both dates are equal. Return value is greater than 0 , if Date is after the date argument. Return value is less than 0, if Date is before the date argument.