如何在特定的日期和时间在Groovy中创建新的日期 [英] How to create new Date in Groovy at specific date and time
问题描述
我想知道有没有其他方法如何在Groovy的特定日期和时间创建新的 Date
而不是从 String
与 Date.parse
方法。可以在Groovy中获得 Date
创建的完整列表吗?
I wonder if there is other way how to create new Date
in Groovy at specific date and time than parse it from String
with Date.parse
method. Can I get complete list of Date
creation in Groovy?
推荐答案
你可以使用现有的Java方法创建一个日期:
You can use the existing Java methods to create a date:
// 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()
方法从地图建立一个日期。你可以这样使用:
Groovy also provides some streamlined extensions for creating Dates. Date.parse()
and Date.parseToStringDate()
parse it from a String. The Date.copyWith()
method builds a date from a map. You can use them like this:
// 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)
这篇关于如何在特定的日期和时间在Groovy中创建新的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!