过去一天我一直试图解决这个问题,似乎无法找到更优雅(Groovy风格)的解决方案,希望有人能帮助我。
基本上我有一个列表,其中包含来自外部源的时间戳(GMT-0),需要转换为指定的时区(在本例中为Paris GMT + 2),然后替换原来的时间戳。以新纪元格式列出新时间(GMT + 2)。
注意:我已尝试使用GregorianCalendar,但未设法弄清楚如何设置输入时区(GMT-0),因此我可以将估算的时间转换为任何时区。
这是我难看的解决方案:
def tStamp= ['2012-06-14T20:16:20Z', '2012-06-14T20:16:40Z', '2012-06-14T20:17:00Z', '2012-06-14T20:17:20Z', '2012-06-14T20:17:40Z', '2012-06-14T20:18:00Z'] println "Ext: "+ tStamp tStamp = tStamp.collect { 7200+(new Date().parse("yyyy-MM-dd'T'HH:mm:ss'Z'", it).time.toString().toLong()/1000).toInteger() } println "New: "+ tStamp Ext: [2012-06-14T20:16:20Z, 2012-06-14T20:16:40Z, 2012-06-14T20:17:00Z, 2012-06-14T20:17:20Z, 2012-06-14T20:17:40Z, 2012-06-14T20:18:00Z] New: [1339704980, 1339705000, 1339705020, 1339705040, 1339705060, 1339705080]
新版本:
def timeStamps =['2012-06-18T09:11:00Z', '2012-06-18T09:11:20Z', '2012-06-18T09:11:40Z']
println "ORIG: "+ timeStamps
// Import the external time: GMT-0
def inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
inputFormat.timeZone = TimeZone.getTimeZone('GMT-0')
timeStamps = timeStamps.collect { inputFormat.parse(it) }
println "IN: "+ timeStamps
// Convert the timez to GMT+2 and conver to epoch
timeStamps = timeStamps.collect { def gcal = new GregorianCalendar(TimeZone.getTimeZone("GMT+2")); gcal.setTime(it); return gcal.getTimeInMillis()/1000 }
println "OUT: "+ timeStamps
ORIG: [2012-06-18T09:11:00Z, 2012-06-18T09:11:20Z, 2012-06-18T09:11:40Z]
IN: [Mon Jun 18 11:11:00 CEST 2012, Mon Jun 18 11:11:20 CEST 2012, Mon Jun 18 11:11:40 CEST 2012]
OUT: [1340010660, 1340010680, 1340010700]
有人有任何建议吗?
提前致以最诚挚的问候和谢意,
塞巴斯蒂安
答案 0 :(得分:1)
您可以使用SimpleDateFormat
来解析和格式化日期。请注意,Date对象对时区一无所知,因此在使用Date对象时,通常不需要担心这些事情,只需从字符串中解析它们或将它们格式化为字符串。
import java.text.SimpleDateFormat
def timeStamps = ['2012-06-14T20:16:20Z', '2012-06-14T20:16:40Z']
def inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
inputFormat.timeZone = TimeZone.getTimeZone('GMT-0')
// Once parsed, dates are agnostic to time zones.
def dates = timeStamps.collect { inputFormat.parse(it) }
def outputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z")
outputFormat.timeZone = TimeZone.getTimeZone('GMT+2')
println "Dates in current locale: " + dates
println "Dates in GMT+2 time zone: " + dates.collect { outputFormat.format(it) }
输出:
Dates in current locale: [Thu Jun 14 17:16:20 ART 2012, Thu Jun 14 17:16:40 ART 2012]
Dates in GMT+2 time zone: [2012-06-14T22:16:20 +0200, 2012-06-14T22:16:40 +0200]
请注意,在第一行中,日期使用当前区域设置打印(在我的机器上是GMT-3 ...这就是为什么它们与输入GMT-0日期之间存在3小时的差异)。
当然,您不需要中间dates
列表,您可以直接执行:
timeStamps.collect { outputFormat.format(inputFormat.parse(it)) }
答案 1 :(得分:0)
我终于找到了解决这个问题的方法:
import java.text.SimpleDateFormat
def convertTimeZone(String time, String sourceTZ, String destTZ) {
final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"
def sdf = new SimpleDateFormat(DATE_TIME_FORMAT)
Date specifiedTime
try {
if (sourceTZ != null){
sdf.setTimeZone(TimeZone.getTimeZone(sourceTZ))
}else{
sdf.setTimeZone(TimeZone.getDefault()) // default to server's timezone
}
specifiedTime = sdf.parse(time)
} catch (Exception e1){
try {
specifiedTime = new Date().parse(DATE_TIME_FORMAT, time)
} catch (Exception e2) {
return time
}
}
// switch timezone
if (destTZ != null){
sdf.setTimeZone(TimeZone.getTimeZone(destTZ))
}else{
sdf.setTimeZone(TimeZone.getDefault()) // default to server's timezone
}
new Date().parse("yyyy-MM-dd'T'HH:mm:ss'Z'", sdf.format(specifiedTime))
}
def timeStamps = ['2012-06-14T20:16:20Z', '2012-06-14T20:16:40Z']
println "IN: "+ timeStamps
timeStamps = timeStamps.collect { ((convertTimeZone(it,"GMT-0","Europe/Paris")).time.toString().toLong()/1000).toInteger() }
println "OUT: "+ timeStamps
输出:
IN: [2012-06-14T20:16:20Z, 2012-06-14T20:16:40Z]
OUT: [1339712180, 1339712200]
希望此解决方案对其他人也有帮助。