如何在日期解析(Java)中忽略时区标识符? [英] How to ignore timezone identifier in date parsing (Java)?

查看:135
本文介绍了如何在日期解析(Java)中忽略时区标识符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试像这样解析日期:

I'm trying to parse date like this:

DateFormat df = new SimpleDateFormat("MMM dd, yyyy K:mm:ss,SSS a z", Locale.ENGLISH);
Date date = df.parse("Oct 04, 2015 2:11:58,757 AM UTC");

由于我居住在UTC + 3时区,所以我的价值是5个小时。但是,我需要将值设置为凌晨2点,并且使用相同的格式字符串(以指定格式给出的日期字符串,不允许更改)。如何执行此操作?

And I'm getting a value of 5 hours am, because i live in UTC+3 timezone. But i need to have value of 2am, however, with the same format string(date string given in a specified format, which i'm not allowed to change). How to do this?

更新:我不需要在适当的时区中格式化日期,我需要按没有时区的值比较这些日期。我想准确地解析该日期,而忽略原始字符串中的时区-并始终处于同一时区(例如,我的时区),无论包含原始字符串的内容是什么:UTC或UTC + 3或其他。

Upd: I don't need to format the date in proper timezone, i need to COMPARE these dates by its values without timezones. I want exactly that date have parsed ignoring the timezone in original string - and be always in the same timezone (my, for example), no matter what contains original string: UTC or UTC+3 or something else.

推荐答案

Upd2:已解决

好的,现在我明白了:

DateFormat df = new SimpleDateFormat("MMM dd, yyyy K:mm:ss,SSS a z", Locale.ENGLISH);
Date date = df.parse("Oct 04, 2015 2:11:58,757 AM UTC");
long diff = TimeZone.getDefault().getRawOffset() - df.getTimeZone().getRawOffset();
date = new Date(date.getTime()-diff);

无论如何,谢谢大家

这篇关于如何在日期解析(Java)中忽略时区标识符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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