Joda Time用时区解析日期并保留该时区 [英] Joda Time parse a date with timezone and retain that timezone

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

问题描述

我想解析一个使用特定时区创建的日期,将其转换为格式并返回。转换有效但时区偏移始终设置为+0000,并根据需要添加/减去时差。我怎样才能让它格式化并保持偏移正确?

I want to parse a date, which was created with a specific timezone, convert it to a format and return it. The conversion works but the timezone offset is always set to +0000 with the time difference being added/subtracted as necessary. How can I get it to format and keep the offset correct?

我期待这个:2012-11-30T12:08:56.23 + 07:00

I expect this: 2012-11-30T12:08:56.23+07:00

但是得到这个:2012-11-30T05:08:56.23 + 00:00

But get this: 2012-11-30T05:08:56.23+00:00

实施:

public static final String ISO_8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSZZ";

public static String formatDateToISO8601Standard(Date date) {
    DateTime dateTime = new DateTime(date);
    DateTimeFormatter df = DateTimeFormat.forPattern(ISO_8601_DATE_FORMAT);
    return dateTime.toString(df);
}

测试类:

private static final String DATE_WITH_TIMEZONE = "30 11 2012 12:08:56.235 +0700";
private static final String EXPECTED_DATE_WITH_TIMEZONE = "2012-11-30T12:08:56.23+07:00";

@Test public void testFormattingDateWithSpecificTimezone() throws Exception {
    String result = JodaDateUtil.formatDateToISO8601Standard(createDate(DATE_WITH_TIMEZONE));
    assertEquals("The date was not converted correctly", EXPECTED_DATE_WITH_TIMEZONE, result); }

private Date createDate(String dateToParse) throws ParseException {
    DateTimeFormatter df = DateTimeFormat.forPattern("dd MM yyyy HH:mm:ss.SSS Z");
    DateTime temp = df.parseDateTime(dateToParse);
    Date date = temp.toDate();
    return date; }


推荐答案

基本上,一旦解析了日期字符串[in你的createDate()方法]你丢失了原来的区域。 Joda-Time 将允许您使用任何区域格式化日期,但您需要保留原始区域。

Basically, once you parse the date string [in your createDate() method] you've lost the original zone. Joda-Time will allow you to format the date using any zone, but you'll need to retain the original zone.

在你的createDate()方法中, DateTimeFormatter df可以返回字符串上的区域。您需要使用 withOffsetParsed() 方法。然后,当您有 DateTime 时,请致电 getZone()。如果您将此区域保存在某处或以某种方式将其传递给格式化例程,则可以通过创建DateTimeFormatterwithZone并在该格式中指定该区域来使用它。

In your createDate() method, the DateTimeFormatter "df" can return the zone that was on the string. You'll need to use the withOffsetParsed() method. Then, when you have your DateTime, call getZone(). If you save this zone somewhere or somehow pass it to your formatting routine, then you can use it there by creating a DateTimeFormatter "withZone" and specifying that zone as the one you want on the format.

作为演示,这里是一个方法中的一些示例代码。希望它能帮助您按照希望的方式更改代码。

As a demo, here's some sample code in a single method. Hopefully, it'll help change your code the way you want it to run.

  public static void testDate() 
  {
    DateTimeFormatter df = DateTimeFormat.forPattern("dd MM yyyy HH:mm:ss.SSS Z");
    DateTime temp = df.withOffsetParsed().parseDateTime("30 11 2012 12:08:56.235 +0700");
    DateTimeZone theZone = temp.getZone();

    Date date = temp.toDate();

    DateTime dateTime = new DateTime(date);
    DateTimeFormatter df2 = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSZZ");
    DateTimeFormatter df3 = df2.withZone(theZone);

    System.out.println(dateTime.toString(df2));
    System.out.println(dateTime.toString(df3));

  }

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

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