-2

How to compare two unix time and check if A unix time is past B unix time? ( if A is in past)

I convert it to Date, but I just need to check if A is in past?

private String millisToDate(String s, long l){
        long unixSeconds = 0;

        if(!TextUtils.isEmpty(s))
            unixSeconds = Long.valueOf(s);
        else if(!TextUtils.isEmpty(String.valueOf(l)))
            unixSeconds = l;


        Date date = new Date(unixSeconds*1000L);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); 

        return sdf.format(date);
    }

I need to check if a unix time is past today date.

I did this: \

private boolean isExpiredDate(String unix) {
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
        //String today = sdf.format(new Date());

        Date today = new Date();
        Date check = new Date(Long.valueOf(unix)*1000L);


        Log.e(TAG, "today: " + today + "|> check: " + check);



        if(check.after(today) || check.equals(today))
            return true;
        else
            return false;
    }
Mr T
  • 1,409
  • 1
  • 17
  • 24
  • 1
    Unclear what you are asking. Maybe you should step back and tell us what kind of code you intend to write, and how it relates to the code you posted. You know, when you already figured that you can "unix time" as a long value; what prevents you from comparing such values?! – GhostCat Oct 24 '16 at 10:52
  • Sorry, I trying to work out how to check if a unix time is past today date. – Mr T Oct 24 '16 at 10:54
  • Your code shows that you know how to convert a "unix" date into a Java Date object. And dates are compared like here: http://stackoverflow.com/questions/2592501/how-to-compare-dates-in-java – GhostCat Oct 24 '16 at 10:58

1 Answers1

1

lets assume that you have two date objects A and B then the way we can test whether A is past according to B by using the following code :

Date A, B;

if(B.before(A))
    // yes A is earlier than B
else 
    // A isn't earlier than B
Ahad
  • 674
  • 1
  • 9
  • 22