数学与MySQL时间戳在Groovy中 [英] Math with MySQL timestamps in groovy

查看:206
本文介绍了数学与MySQL时间戳在Groovy中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从常规中减去另一个时间戳,并获得已经过去的小时数。时间戳来自MySQL。



endDate - startDate
给出四舍五入整数



我希望得到2.35小时的结果等等。

解决方案

您可以使用 groovy.time.TimeCategory 就像这样:

  //创建两个时间戳
import java .sql.Timestamp

def dates = ['2012/08/03 09:00','2012/08/03 10:30']
def(时间戳a,时间戳b) = date.collect {
New Timestamp(Date.parse('yyyy / MM / dd hh:mm',it).time)
}

//然后比较它们与TimeCategory
使用(groovy.time.TimeCategory){
def duration = b - a
println$ {duration.days} d $ {duration.hours} h $ {duration.minutes } m

将打印:

  0d 1h 30m 


I need to subtract one timestamp from another in groovy and get the number of hours that has passed. The timestamps are coming from MySQL. When I do simple math I get numbers of days rounded off to zero integers.

endDate - startDate gives rounded integer

I want a result of 2.35 hours, etc.

解决方案

You can use groovy.time.TimeCategory like so:

// create two timestamps
import java.sql.Timestamp

def dates = ['2012/08/03 09:00', '2012/08/03 10:30']
def (Timestamp a, Timestamp b) = dates.collect {
  new Timestamp( Date.parse( 'yyyy/MM/dd hh:mm', it ).time )
}

// Then compare them with TimeCategory
use(groovy.time.TimeCategory) {
  def duration = b - a
  println "${duration.days}d ${duration.hours}h ${duration.minutes}m"
}

Which will print:

0d 1h 30m

这篇关于数学与MySQL时间戳在Groovy中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆