[ZZ]Groovy Goodness: Date and Time Durations and the TimeCategory

Groovy has some elegant ways to work with date and time values. One of them is the support of durations. We can define a duration to denote a certain time amount, like 7 days, 2 hours and 50 minutes. We can use these durations to add or subtract them from date and time objects.

The TimeCategory provides an even Groovier way to work with durations. We can use constructs like 7.days + 12.minutes to create a duration. When we read this code it is just like reading English text. Here is some sample code:


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> import  groovy.time. *
import  org.codehaus.groovy.runtime.TimeCategory

//  Define period of 2 years, 3 months, 15 days, 0 hours, 23 minutes, 2 seconds and 0 milliseconds.
def period  =   new  DatumDependentDuration( 2 3 15 0 23 2 0 )
assert   ' 2 years, 3 months, 15 days, 23 minutes, 2.000 seconds '   ==  period.toString()
def year2000 
=   new  Date( 100 0 0 )   //  Jan 1, 2000
assert   ' Mon Apr 15 00:23:02 UTC 2002 '   ==  (period  +  year2000).toString()

//  Define time period of 5 hours, 54 minutes and 30 milliseconds.
def time  =   new  TimeDuration( 5 54 0 30 )
assert   ' 5 hours, 54 minutes, 0.030 seconds '   ==  time.toString()

use (TimeCategory) {
    
assert  period.toString()  ==  ( 2 .years  +   3 .months  +   15 .days  +   0 .hour  +   23 .minutes  +   2 .seconds).toString()
    
assert  time.toString()  ==  ( 5 .hours  +   54 .minutes  +   30 .milliseconds).toString()

    
//  We can use period.from.now syntax.    
    def d1  =   1 .week  -   1 .day
    def d2 
=   new  Date()  +   6 .days
    
assert  d2.format( ' yyyy-MM-dd ' ==  d1.from.now.toString()
    
    
//  We can use period.ago syntax.
    def d3  =   3 .days.ago
    def d4 
=   new  Date()  -   3
    
assert  d4.format( ' yyyy-MM-dd ' ==  d3.toString()
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值