2012-08-02 22 views
1

我需要從groovy中的另一個時間戳中減去一個時間戳,並獲取已經過去的小時數。時間戳來自MySQL。當我做簡單的數學運算時,我得到的天數舍入爲零整數。在Groovy中使用MySQL時間戳的數學

結束日期 - 的startDate 給人圓潤的整數

我想爲2.35小時,結果等

+0

_SELECT(endDate - startDate)/ 60 FROM table_ does ret好的結果? – 2012-08-02 20:30:02

回答

0

您可以使用groovy.time.TimeCategory像這樣:

// 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" 
} 

,它將打印:

0d 1h 30m