1

我需要显示经过的时间。我有以下格式的日期。

Date1 = Thu May 23 10:10:10 EDT 2013
Date2 = Tue May 21 10:10:10 EDT 2013

我目前做了TimeDuration duration=TimeCategory.minus(now,LaunchTime)

我的输出显示类似2 days, 23 minutes, 25.154 seconds

我想展示的不是2 days, 23 minutes, 25.154 seconds48:23:25以小时和分钟为单位)。

4

1 回答 1

3

您可以执行以下操作来创建一个新的 TimeDuration ,将天数转换为小时数:

import groovy.time.TimeDuration
import groovy.time.TimeCategory

date1 = Date.parseToStringDate( 'Thu May 23 10:10:10 EDT 2013' )
date2 = Date.parseToStringDate( 'Tue May 21 12:14:10 EDT 2013' )

// Normalize method to return a new TimeDuration
TimeDuration normalize( TimeDuration tc ) {
  new TimeDuration( ( tc.days != 0 ? tc.days * 24 : 0 ) + tc.hours,
                    tc.minutes, tc.seconds, tc.millis )
}

// Then use the category to subtract the dates, and call normalize
TimeDuration normalized = use( groovy.time.TimeCategory ) {
  normalize( date1 - date2 )
}

println normalized
于 2013-05-23T14:44:33.497 回答