How to calculate the difference between two Java java.sql.Timestamps?

前端 未结 5 1124
悲&欢浪女
悲&欢浪女 2020-12-14 19:53

Please include the nanos, otherwise it would be trivial:

long diff = Math.abs(t1.getTime () - t2.getTime ());

[EDIT] I want the most precis

5条回答
  •  别那么骄傲
    2020-12-14 20:15

    In what units? your diff above will give milliseconds, Timestamp.nanos() returns an int, which would be in (millionths?) of a millisecond.So do you mean e.g.

    (t1.getTime () + (.000001*t1.getNanos()) - (t2.getTime () + (.000001*t2.getNanos())
    

    or am I missing something? Another question is do you need this level of precision? AFAIK the JVM isn't guaranteed to be precise at this level, I don't think it'd matter unless you're sure your datasource is that precise.

提交回复
热议问题