与Joda分析日期与时区 [英] Parsing date with Joda with time zone

查看:134
本文介绍了与Joda分析日期与时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个时间戳描述了两种不同格式的相同时刻。

I have two timestamps which describe the same instant of time in two different formats.

2010-10-03 18:58:07 2010-10-03T16:58:07.000 + 02:00

我用Joda分析两个不同的日期格式化程序的时间戳。最后我想要有两个相同时间的DateTime对象。

I parse the timestamps with two different date formatters with Joda. In the end i want to have two DateTime objects that are equal in terms of being the same instant of time.

DateFormatter提供了几种控制时区和区域设置的方法,但是我不能让它上班

The DateFormatter offers several methods to control time zones and locales but i couldn't get it to work.

这是我想要工作的代码:

This is the code that i would like to work:

    final String date1 = "2010-10-03 18:58:07"; // Europe/Berlin local time
    final DateTimeFormatter formatter1 = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    final DateTime dateTime1 = formatter1.parseDateTime(date1);

    final String date2 = "2010-10-03T16:58:07.000+02:00"; // Europe/Berlin local time with time zone
    final DateTimeFormatter formatter2 = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    final DateTime dateTime2 = formatter2.parseDateTime(date2);

    Assert.assertTrue(dateTime1.isEqual(dateTime2));

提前感谢,如果有人可以帮助我!

Thanks in advance if somebody can help me!

推荐答案

如果您的默认时间是欧洲/柏林,2010-10-03 18:58:07对应于2010-10-03T16:58:07.000 + 00:00。

If your default time zome is Europe/Berlin, 2010-10-03 18:58:07 corresponds to 2010-10-03T16:58:07.000+00:00.

您可能误解字符串表示中的时区字段。您的时间戳2010-10-03T16:58:07.000 + 02:00表示在GMT时间偏离+2小时的时区为16:58:07),或其他措词现在是16 :58:07在柏林我假设你的意思是说是格林尼治标准时间16:58:07?

You probably misunderstand the time zone field in the string representation. Your time stamp 2010-10-03T16:58:07.000+02:00 means that "it is 16:58:07 in a time zone with a +2 hour offset from GMT), or in an other wording "it is now 16:58:07 in Berlin". I assume that you expected it to mean that it's 16:58:07 GMT?

这篇关于与Joda分析日期与时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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