如何在特定的日期和时间在Groovy中创建新的日期

我想知道有没有其他方式如何在Groovy中创建新的Date,而不是用DateParse方法从String解析它。可以在Groovy中获取完整的日期创建列表吗?

解决方法

您可以使用现有的Java方法来创建日期:

// takes the date encoded as milliseconds since midnight,January 1,1970 UTC
def mydate = new Date(System.currentTimeMillis())

// create from an existing Calendar object
def mydate = new GregorianCalendar(2014,Calendar.APRIL,3,1,23,45).time

Groovy还为创建日期提供了一些简化的扩展。 Date.parse()Date.parseToStringDate()从一个String解析它。 Date.copyWith()方法从地图构建日期。你可以这样使用它们:

// uses the format strings from Java's SimpleDateFormat
def mydate = Date.parse("yyyy-MM-dd hh:mm:ss","2014-04-03 1:23:45")

// uses a format equivalent to EEE MMM dd HH:mm:ss zzz yyyy
def mydate = Date.parseToStringDate("Thu Apr 03 01:23:45 UTC 2014")

def mydate = new Date().copyWith(
    year: 2014,month: Calendar.APRIL,dayOfMonth: 3,hourOfDay: 1,minute: 23,second: 45)

相关文章

背景:    8月29日,凌晨4点左右,某服务告警,其中一个...
https://support.smartbear.comeadyapi/docs/soapui/steps/g...
有几个选项可用于执行自定义JMeter脚本并扩展基线JMeter功能...
Scala和Java为静态语言,Groovy为动态语言Scala:函数式编程,...
出处:https://www.jianshu.com/p/ce6f8a1f66f4一、一些内部...
在运行groovy的junit方法时,报了这个错误:java.lang.Excep...