Java - System.currentTimeMillis(); Not Returning Difference

后端 未结 3 1312
长发绾君心
长发绾君心 2021-01-20 17:02

For some reason I am getting a consistent 3600 returned by the function:

private static long getCountdownLeft() {
    long now = System.currentTimeMillis();
         


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-20 17:55

    Hey I have just added print statements for prev, new and elapsed variables. The values are

    previous time 1343882409054

    present time 1343882409054

    elapsed time 0

    Time is 3600

    So your millisLeft will always be 3600 .

    But if you try using

    System.nanoTime()

    the values are,

    previous time 519222175869357

    present time 519222175923421

    elapsed time 54064

    Time is 3545

    So you have to be more granular here and consider using System.nanoTime().

提交回复
热议问题