您可以使用现有的 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() http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Date.html#parse(java.lang.String,%20java.lang.String) and Date.parseToStringDate() http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Date.html#parseToStringDate(java.lang.String)从字符串中解析它。这Date.copyWith() http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Date.html#copyWith(java.util.Map)方法从地图构建日期。你可以像这样使用它们:
// 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)