gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:48:48 27 4
gpt4 key购买 nike

我想知道是否有其他方法如何创建新 Date在特定日期和时间的 Groovy 中,而不是从 String 解析它与 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() Date.parseToStringDate() 从字符串解析它。 Date.copyWith() 方法从 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)

关于date - 如何在特定日期和时间在 Groovy 中创建新日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22848000/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com