簡體   English   中英

在Java中獲得兩個Timestamp對象之間的差異

[英]Getting difference between two Timestamp objects in Java

我正在做一個項目,它是關於停車的,它包含一個數據庫。 當汽車進入停車場時,我有可變的時間戳記用於時間/日期,對於汽車在出口時,我也具有可變的時間戳記。 我想獲得小時數差異(每整小時后的分鍾數是一個新小時,等等。4小時2分鍾與5小時相同),因此我可以計算服務價格(小時數乘以每小時價格)。

請幫助我,並提前謝謝

得到毫秒差並計算如下

long timeDiff = endTime.getTime() - startTime.getTime()

int hours = (int)Math.ceil(timeDiff/1000*60*60) //In here we are converting milli seconds difference to hours and then if it is not an exact number we are converting that to the next integer

在獲得以毫秒為單位的時間值(例如,使用Date對象)之后,請使用TimeUnit獲得時間差:

TimeUnit.HOURS.convert(timeOut.getTime()-timeIn.getTime(), TimeUnit.MILLISECONDS);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM