开发者

Android comparing time and milliseconds

开发者 https://www.devze.com 2023-02-22 05:04 出处:网络
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.

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消