Nilsymbol
Nilsymbol

Reputation: 525

System.currentTimeMillis() only updating every 128 seconds?

I have a loop in which I am checking seconds elapsed by doing t-(long)(System.currentTimeMillis()/1000.0f) where t is set to t=(long)(System.currentTimeMillis()/1000.0f) right before the loop. I find that (long)(System.currentTimeMillis()/1000.0f) is equal to t for the first 128 seconds. It then updates after another 128 seconds. I am doing this on a background thread. What am I doing wrong?

Upvotes: 0

Views: 199

Answers (2)

Randyka Yudhistira
Randyka Yudhistira

Reputation: 3652

You can use this to convert milis to second

long timeMillis = System.currentTimeMillis();
long timeSeconds = TimeUnit.MILLISECONDS.toSeconds(timeMillis);

Upvotes: 2

Atsby
Atsby

Reputation: 2327

System.currentTimeMillis() is a sufficiently large value that float cannot represent all integers around that range.

The fix is to simply use integer division by 1000.

Upvotes: 3

Related Questions