简体   繁体   English

在Java中获得两个Timestamp对象之间的差异

[英]Getting difference between two Timestamp objects in Java

I'm working on a project, it's about parking and it contains a database. 我正在做一个项目,它是关于停车的,它包含一个数据库。 I have variable timestamp for time/date when car enter the parking, and one also for when the car is on the exit. 当汽车进入停车场时,我有可变的时间戳记用于时间/日期,对于汽车在出口时,我也具有可变的时间戳记。 I want to get difference in hours (minute after every full hour is a new hour etc. 4 hours and 2 mins are same as 5 hours) so I can calculate price of the service (hours multiply by price per hour). 我想获得小时数差异(每整小时后的分钟数是一个新小时,等等。4小时2分钟与5小时相同),因此我可以计算服务价格(小时数乘以每小时价格)。

Help me pls, and thank you in advance 请帮助我,并提前谢谢

get difference in milliseconds and calculate as below 得到毫秒差并计算如下

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