【发布时间】:2016-09-26 07:16:24
【问题描述】:
我有两个 Timestamp 对象,一个用于当前时间,一个用于过去某个时间。如何计算两个时间戳之间经过的小时数?
java.sql.Timestamp currentTS = Timestamp(Calendar.getInstance().get(Calendar.MILLISECOND));
java.sql.Timestamp lastProcessedTS = Timestmp.valueOf(<some value in past>);
【问题讨论】:
-
你想用java还是SQL计算持续时间?如果你想用 Java 来做,并且你有 1.8,试试这个:docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html 和那个:docs.oracle.com/javase/tutorial/datetime/iso/period.html。基本上你可以使用 long ns = Duration.between(t1, t2).toNanos() 来获得 Nanos 的差异,这里解释了 datetime-instant-conversion:stackoverflow.com/questions/19431234/…。如果您想使用 sql,请指定您的 DB/sql 方言。
标签: java sql date time calendar