How to calculate time difference in java?

后端 未结 17 1585
醉酒成梦
醉酒成梦 2020-11-22 16:33

I want to subtract two timeperiods say 16:00:00 from 19:00:00. Is there any java function for this? The results can be in milliseconds, seconds, or minutes.

17条回答
  •  星月不相逢
    2020-11-22 16:55

    Just like any other language; convert your time periods to a unix timestamp (ie, seconds since the Unix epoch) and then simply subtract. Then, the resulting seconds should be used as a new unix timestamp and read formatted in whatever format you want.

    Ah, give the above poster (genesiss) his due credit, code's always handy ;) Though, you now have an explanation as well :)

提交回复
热议问题