我有一个时间戳(submitTime),我需要检查它是否不到1小时。时间戳以微秒为单位,包括日期。
currentTime = 1527530605357000000 (2018年5月28日星期一下午6:03:25.357 )
submitTime = 1527529918658907821 (2018年5月28日星期一下午5:51:58.659 )
long currentTime = (long) (new Date().getTime()*1000000)
submitTime = job.SubmitTime // part of the code
oneHhour = 3600000000
if (currentTime - submitTime > oneHhour) {
println job.Name + " env is up more than 1 hour";但它不起作用,因为结果是686698092179,而且它不代表时间。帮助?
发布于 2018-05-29 02:39:39
假设SubmitTime是以微秒为单位的时间戳,您可以将其与当前以微秒为单位的时间戳进行比较,如下所示:
// Get the current time (System.currentTimeMillis) in microseconds:
long currentMicroseconds = TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis())
// You could also simply do this:
long currentMicroseconds = System.currentTimeMillis() * 1000
// Subtract the timestamps and compare:
if (currentMicroseconds - job.SubmitTime > 3600000000) {
// More than an hour has elapsed
}时间戳假定为自1970年1月1日格林尼治标准时间00:00:00以来的微秒数(与Date.getTime一致)。
https://stackoverflow.com/questions/50571881
复制相似问题