jodatime java8交互,从Joda时间库迁移到Java时间(Java 8)

I am trying to migrate from Joda time library to Java time (Java 8).

I am unable to find equivalent of ISODateTimeFormat.dateOptionalTimeParser() in java.time

Joda ISO formatter has nice parsers:

ISODateTimeFormat.dateTimeParser() : generic - selects parser based on string parsed.

Similarly:

ISODateTimeFormat.dateOptionalTimeParser().

I am finding it difficult to change Joda time to java.time.

Can some one guide me?

example:

String dateTimeString = "2015-01-01T12:29:22+00:00";

String dateTimeString2 = "2015-01-01T12:29:22";

When I parse this string using joda time then

ISODateTimeFormat.dateTimeParser().withZone("EST")

can handle both without as problem. Which is equivalent of this in java time?

Using java 8, ZonedDateTime with ISO_Zoned_date_time is not able to handle both.

解决方案

You cannot use a predefined formatter but you can construct your own one (and assign it to a static constant) using following pattern:

static final DateTimeFormatter DATE_TIME_OPTIONAL_OFFSET =

DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss[xxx]");

Attention: If you parse an input containing only date and time but without offset (and without any offset/zone-default) then the result can only be a LocalDateTime, not a global timestamp.

Please also note the different behaviour of method withZone(...).

Joda-Time

When parsing, this zone will be set on the parsed datetime.

A null zone means of no-override. If both an override chronology

and an override zone are set, the override zone will take precedence

over the zone in the chronology.

Java-8 (JSR-310)

When parsing, there are two distinct cases to consider.

If a zone has been parsed directly from the text, perhaps because

DateTimeFormatterBuilder.appendZoneId() was used, then this override Zone

has no effect. If no zone has been parsed, then this override zone will

be included in the result of the parse where it can be used to build

instants and date-times.

Side remark: The Joda-Time-method withOffsetParsed() is closer to Java-8-behaviour.

Update: I have now done my own tests. See the sometimes surprising results.

System.out.println(System.getProperty("java.version")); // 1.8.0_31

// parsing s1 with offset = UTC

String s1 = "2015-01-01T12:29:22+00:00";

OffsetDateTime odt1 = DATE_TIME_OPTIONAL_OFFSET.parse(s1, OffsetDateTime::from);

System.out.println(odt1); // 2015-01-01T12:29:22Z --- OK

LocalDateTime ldt1 = DATE_TIME_OPTIONAL_OFFSET.parse(s1, LocalDateTime::from);

System.out.println(ldt1); // 2015-01-01T12:29:22 --- OK

ZonedDateTime zdt1 = DATE_TIME_OPTIONAL_OFFSET.withZone(ZoneId.of("America/New_York")).parse(s1, ZonedDateTime::from);

System.out.println(zdt1); // 2015-01-01T12:29:22-05:00[America/New_York] --- seems to be a bug compared with the spec above, the parsed offset was overridden!!!

// now parsing s2 without offset

String s2 = "2015-01-01T12:29:22";

OffsetDateTime odt2 = DATE_TIME_OPTIONAL_OFFSET.parse(s2, OffsetDateTime::from);

System.out.println(odt2); // 2015-01-01T12:29:22Z --- questionable, the offset Z is invented/guessed here

LocalDateTime ldt2 = DATE_TIME_OPTIONAL_OFFSET.parse(s2, LocalDateTime::from);

System.out.println(ldt2); // 2015-01-01T12:29:22 --- OK

DATE_TIME_OPTIONAL_OFFSET.withZone(ZoneId.of("America/New_York")).parse(s2, ZonedDateTime::from);

// throws an exception --- seems to be a bug compared with the spec above, the zone set was not accepted

Conclusion:

I would be careful when migrating. The devil is in the details. Maybe a newer Java-version 8u40 has meanwhile corrected some of the problems shown (at least the behaviour of withZone() is probably corrected - see JDK-issue 8033662, but for 8u31 the backport fix appears to be missing?!). You should also note that your "timezone" labelled "EST" was replaced by "America/New_York" in my tests because "EST" is not a recognized timezone id (it is rather a localized timezone name abbreviation in US).

Update - final solution

After extra testing this code seems to work in Java 8u31 (assuming UTC as default in case of missing offset in input):

static final DateTimeFormatter DATE_TIME_OPTIONAL_OFFSET =

DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss[xxx]");

OffsetDateTime odt =

DATE_TIME_OPTIONAL_OFFSET.withZone(ZoneOffset.UTC).parse(input, OffsetDateTime::from);

ZonedDateTime zdt = odt.toZonedDateTime(); // containing a fixed offset

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值