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

2024-01-10

我想知道是否还有其他方法如何创建新的Date在特定日期和时间的 Groovy 中,而不是从中解析它String with Date.parse方法。我可以得到完整的清单吗Date在 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() 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐