Moe
Moe

Reputation: 1557

Android comparing time and milliseconds

I'm trying to compare time in milliseconds in Android with the system time. startDate and endDate are all long and represent timestamps in milliseconds.

if (startDate <= System.currentTimeMillis()  >= endDate)

This is the error I'm getting:

The operator >= is undefined for the argument type(s) boolean, long

Upvotes: 1

Views: 4318

Answers (1)

Chris Thompson
Chris Thompson

Reputation: 35598

You need to change it to

if (startDate <= System.currentTimeMillis() && System.currentTimeMillis() >= endDate)

The reason for this is because the statements get evaluated like so:

startDate <= System.currentTimeMillis();
<result of above> >= endDate;

or equivalently

(startDate <= System.currentTimeMillis()) <= endDate

The <= operator results in a boolean value and then what you have is

boolean <= long

which you can't do. Unfortunately in Java, you can't chain operations together like that because they are evaluated one at a time and then the result of the first is used as input to the second, and so on.

Upvotes: 2

Related Questions