Date 添加秒和纪元以获取日期时间
这里是初学者,尝试将整数数组(表示秒)添加到历代数组中: 样本输入:Date 添加秒和纪元以获取日期时间,date,groovy,time,Date,Groovy,Time,这里是初学者,尝试将整数数组(表示秒)添加到历代数组中: 样本输入: AddSeconds = [3,4] TimeEpoch = [1575165652000, 1576424223000] // Which are 2019-12-01 02:00:52 and 2019-12-15 15:37:03 期望输出: endDate = [2019-12-01 02:00:55, 2019-12-15 15:37:07] 我需要将TimeEpoch转换为“yyyy-MM-dd hh:MM:s
AddSeconds = [3,4]
TimeEpoch = [1575165652000, 1576424223000] // Which are 2019-12-01 02:00:52 and 2019-12-15 15:37:03
期望输出:
endDate = [2019-12-01 02:00:55, 2019-12-15 15:37:07]
transpose()
将它们连接在一起:
然后,对于每一个,我们创建一个瞬间,添加秒,并使用当前的系统时区将其转换为LoalDATeTime--需要考虑时区
addSeconds = [3,4]
timeEpoch = [1575165652000, 1576424223000]
import java.time.*
import java.time.format.*
def formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")
def datesAsStrings = [addSeconds, timeEpoch]
.transpose()
.collect { a, t -> Instant.ofEpochMilli(t).plusSeconds(a).atZone(ZoneId.systemDefault()).toLocalDateTime() }
.collect { d -> d.format(formatter) }
datesAsStrings.each { println it }
[ [3, 1575165652000], [4, 1576424223000] ]