如何在 groovy 中从当前日期添加年份或月份?
how to add year or months from current date in groovy?
如何在 groovy 脚本中将当前日期加一年?
def Format1 = "yyyy-MM-dd"
def today = new Date()
def currentDate = today.format(Format1)
示例:2015-07-29 到 2016-07-29 和 2015-07-29 到 2015-10-29。
使用TimeCategory
.
import groovy.time.TimeCategory
def acceptedFormat = "yyyy-MM-dd"
def today = new Date() + 1
def currentdate = today.format(acceptedFormat)
use(TimeCategory) {
def oneYear = today + 1.year
println oneYear
def ninetyDays = today + 90.days
println ninetyDays
}
可以在 The Groovy Pimp my Library Pattern 上的文档中找到有关其工作原理的更多信息。简而言之,Integer
class 在 use
块中得到了丰富,为其提供了额外的方法,使日期操作非常方便。
请注意,+
(或 plus
)运算符已经可以处理常规整数,但默认情况下是添加一天。 (因此,new Date() + 1
将在 24 小时内为您提供日期)
如何在 groovy 脚本中将当前日期加一年?
def Format1 = "yyyy-MM-dd"
def today = new Date()
def currentDate = today.format(Format1)
示例:2015-07-29 到 2016-07-29 和 2015-07-29 到 2015-10-29。
使用TimeCategory
.
import groovy.time.TimeCategory
def acceptedFormat = "yyyy-MM-dd"
def today = new Date() + 1
def currentdate = today.format(acceptedFormat)
use(TimeCategory) {
def oneYear = today + 1.year
println oneYear
def ninetyDays = today + 90.days
println ninetyDays
}
可以在 The Groovy Pimp my Library Pattern 上的文档中找到有关其工作原理的更多信息。简而言之,Integer
class 在 use
块中得到了丰富,为其提供了额外的方法,使日期操作非常方便。
请注意,+
(或 plus
)运算符已经可以处理常规整数,但默认情况下是添加一天。 (因此,new Date() + 1
将在 24 小时内为您提供日期)