在Groovy/Java中将UTC日期转换为GMT
我正在与SoupUI a合作,我需要调整我在回复GMT日期/时间时返回的日期/时间(UTC)。我返回respone的日期如下所示:在Groovy/Java中将UTC日期转换为GMT,groovy,utc,datetime-format,gmt,date-conversion,Groovy,Utc,Datetime Format,Gmt,Date Conversion,我正在与SoupUI a合作,我需要调整我在回复GMT日期/时间时返回的日期/时间(UTC)。我返回respone的日期如下所示: 2012-11-09T00:00:00+01:00 我想把这个转换成 2012-11-08T23:00:00Z 不幸的是,我缺乏Java技能,因此也缺乏Groovy技能,无法独立完成这项任务。我做了很多关于日期转换的搜索,但直到现在我仍然找不到我想要的。我会继续寻找。如果我真的找到了解决方案,那么我会把它贴在这里。假设时区部分没有冒号,我相信这应该可以: //
2012-11-09T00:00:00+01:00
我想把这个转换成
2012-11-08T23:00:00Z
不幸的是,我缺乏Java技能,因此也缺乏Groovy技能,无法独立完成这项任务。我做了很多关于日期转换的搜索,但直到现在我仍然找不到我想要的。我会继续寻找。如果我真的找到了解决方案,那么我会把它贴在这里。假设时区部分没有冒号,我相信这应该可以:
// Your input String (with no colons in the timezone portion)
String original = '2012-11-09T00:00:00+0100'
// The format to read this input String
def inFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssZ" )
// The format we want to output
def outFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss'Z'" )
// Set the timezone for the output
outFormat.timeZone = java.util.TimeZone.getTimeZone( 'GMT' )
// Then parse the original String, and format the resultant
// Date back into a new String
String result = outFormat.format( inFormat.parse( original ) )
// Check it's what we wanted
assert result == '2012-11-08T23:00:00Z'
如果时区中有冒号,则需要执行此任务(或者可能需要一个日期处理框架,如JodaTime),并且可以将前两行更改为:
// Your input String
String original = '2012-11-09T00:00:00+01:00'
// The format to read this input String (using the X
// placeholder for ISO time difference)
def inFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssX" )
假设时区部分中没有冒号,我相信这应该可以:
// Your input String (with no colons in the timezone portion)
String original = '2012-11-09T00:00:00+0100'
// The format to read this input String
def inFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssZ" )
// The format we want to output
def outFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss'Z'" )
// Set the timezone for the output
outFormat.timeZone = java.util.TimeZone.getTimeZone( 'GMT' )
// Then parse the original String, and format the resultant
// Date back into a new String
String result = outFormat.format( inFormat.parse( original ) )
// Check it's what we wanted
assert result == '2012-11-08T23:00:00Z'
如果时区中有冒号,则需要执行此任务(或者可能需要一个日期处理框架,如JodaTime),并且可以将前两行更改为:
// Your input String
String original = '2012-11-09T00:00:00+01:00'
// The format to read this input String (using the X
// placeholder for ISO time difference)
def inFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssX" )
谢谢,这正是我需要它做的。谢谢,这正是我需要它做的。欢迎使用堆栈溢出!我们鼓励你这样做。如果您有,请将其添加到问题中-如果没有,请先研究并尝试您的问题,然后再回来。欢迎使用堆栈溢出!我们鼓励你这样做。如果您有,请将其添加到问题中-如果没有,请先研究并尝试您的问题,然后再回来。