从标准时区到另一时区的时间转换

问题描述 投票:0回答:1

我正在尝试将时间从一个时区转换为另一个时区。

起初,我有时间和时区。我想将其转换为其他时区。例如:时区:格林尼治标准时间StartDateTime:2020-01-15T08:30:00.000Z

现在我想将其转换为IST时区

代码:

def oldTimezone = context.expand('${#Project#Timezone}')
def SDT = context.expand('${#Project#StartDateTime}') 
startDate =  new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(SDT)
log.info 'time : ' + startDate 
def dateformat =  new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
dateformat.setTimeZone(TimeZone.getTimeZone(oldTimezone));
def currentTimeinUTC = dateformat.format(startDate);
log.info 'time set in first timezone : ' + currentTimeinUTC

输出

Wed Jan 15 14:43:32 IST 2020:INFO:time : Wed Jan 15 08:30:00 IST 2020
Wed Jan 15 14:56:16 IST 2020:INFO:time set in first timezone : 2020-01-15T03:00:00.000Z

如果这将返回输出'2020-01-15T08:30:00.000Z',那么我将以相同的准确方式将其转换为IST。但问题是,它了解到原始时间在IST中并将其转换为UTC。

groovy timezone soapui
1个回答
2
投票

避免使用旧的日期类,它们会使所有事情变得困难...

解析该UTC时间并将其转换为IST时间很简单:

import java.time.*

def parsedUTCTime = ZonedDateTime.parse('2020-01-15T08:30:00.000Z')

def istTime = parsedUTCTime.withZoneSameInstant(TimeZone.getTimeZone("IST").toZoneId())
© www.soinside.com 2019 - 2024. All rights reserved.