如何格式化Groovy中的持续时间?

时间:2013-05-23 14:36:20

标签: parsing time groovy format

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

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 seconds,而不是48:23:25(以小时和分钟为单位)。

1 个答案:

答案 0 :(得分: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