Groovy - 分钟的时间戳
Groovy - timestamp from minutes
我有一个数组或 times/values 返回到一个数组中,例如:
[0, 60]
其中时间以分钟为单位,0 = 12:00 a.m, 60 = 1:00 a.m。我想将这些作为时间戳存储在 oracle 数据库中。如何将分钟转换为 groovy 中的时间戳?
编辑:*日期可以是任意的,我只需要时间部分用于我的目的。
编辑:* 对于上面的示例,我需要两个时间戳,可能与此类似:
1/1/1970 12:00:00.000000 AM // for 0
1/1/1970 1:00:00.000000 AM // for 60
编辑:
到目前为止我可以得到以下信息:
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
def date = new Date(0)
给我:
Thu Jan 01 00:00:00 UTC 1970
那么现在如何根据以分钟为单位的值设置时间?
我假设您想将当前日期偏移量与给定时间戳的分钟数一起使用。
由于新的日期或时间戳将被初始化为当前时间和日期,您可以使用它并使用数组中的值覆盖分钟字段。值 60 和更大的值将自动转入小时、天等。这是一种方法:
def times = [0, 60]
def dates = times.collect { new Date().clearTime().copyWith(minute: it) }
def timestamps = dates.collect { new java.sql.Timestamp(it.time) }
println timestamps
// output: [2015-06-23 00:00:00.0, 2015-06-23 01:00:00.0]
copyWith
方法是在 groovy 2.2.0 中添加的。在旧版本中,你可以使用这样的东西:
def d = new Date().clearTime()
d.minutes = 60
我有一个数组或 times/values 返回到一个数组中,例如:
[0, 60]
其中时间以分钟为单位,0 = 12:00 a.m, 60 = 1:00 a.m。我想将这些作为时间戳存储在 oracle 数据库中。如何将分钟转换为 groovy 中的时间戳?
编辑:*日期可以是任意的,我只需要时间部分用于我的目的。 编辑:* 对于上面的示例,我需要两个时间戳,可能与此类似:
1/1/1970 12:00:00.000000 AM // for 0
1/1/1970 1:00:00.000000 AM // for 60
编辑:
到目前为止我可以得到以下信息:
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
def date = new Date(0)
给我:
Thu Jan 01 00:00:00 UTC 1970
那么现在如何根据以分钟为单位的值设置时间?
我假设您想将当前日期偏移量与给定时间戳的分钟数一起使用。
由于新的日期或时间戳将被初始化为当前时间和日期,您可以使用它并使用数组中的值覆盖分钟字段。值 60 和更大的值将自动转入小时、天等。这是一种方法:
def times = [0, 60]
def dates = times.collect { new Date().clearTime().copyWith(minute: it) }
def timestamps = dates.collect { new java.sql.Timestamp(it.time) }
println timestamps
// output: [2015-06-23 00:00:00.0, 2015-06-23 01:00:00.0]
copyWith
方法是在 groovy 2.2.0 中添加的。在旧版本中,你可以使用这样的东西:
def d = new Date().clearTime()
d.minutes = 60