我想知道有没有其他方式如何在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)