在Groovy中为Jira解析日期字符串而不会丢失时区 [英] Parse date string without losing timezone in Groovy for Jira

查看:72
本文介绍了在Groovy中为Jira解析日期字符串而不会丢失时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的日期字符串具有不同的时区,我需要以不同的格式显示它们,但仍显示每个人的原始时区.

I have date strings being created with different timezones, and I need to display them in a different format, but still showing each one's original timezone.

我能够解析,但是它解析为unix时间戳,然后失去了原始时区.

I'm able to parse, but it parses to a unix timestamp, which then loses the original timezone.

def dateCreated = issue.fields.created
// 2018-12-21T10:20:00.483-0800

def dateParsed = Date.parse("yyyy-MM-dd'T'HH:mm:ss.SSSz", dateCreated)
// 1545416400483

def dateFormatted = dateParsed.format('yyyy-MM-dd h:mm a, z')
// 2018-12-21 6:20 PM, UTC

是否有一种方法可以直接解析/格式化为所需的格式而又不会丢失中间的时区?

Is there a way to parse/format straight to the desired format without losing the timezone in the middle?

推荐答案

好吧,我意识到这实际上并不能满足我的需求.原始字符串使用-0800,它是一个偏移量,而不是时区.因此,我在API响应中的其他地方进行了查找,并找到了创建问题的人所在的时区.我用它来正确格式化日期.

Okay, I realized that this wouldn't actually get me what I want. The original string uses -0800, which is an offset, not a timezone. So I looked elsewhere in the API response and found the timezone of the person who created the issue. I used this to format the date properly.

最终代码:

def dateCreated = issue.fields.created
// 2018-12-21T10:20:00.483-0800

def creatorTimeZone = issue.fields.creator.timeZone
// America/Los_Angeles

def dateParsed = Date.parse("yyyy-MM-dd'T'HH:mm:ss.SSSz", dateCreated)
// 1545416400483

def dateFormatted = dateParsed.format('yyyy-MM-dd h:mm a, z', TimeZone.getTimeZone(creatorTimeZone))
// 2018-12-21 10:20 AM, PST

这篇关于在Groovy中为Jira解析日期字符串而不会丢失时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆