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()
}
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()
}
本文介绍Groovy中优雅处理日期时间的方法,包括定义和使用持续时间。通过具体代码示例展示了如何创建持续时间并进行加减运算。
701

被折叠的 条评论
为什么被折叠?



