Java日期和utcTimeOffset [英] Java Date and utcTimeOffset

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

问题描述

我有一个Java日期( java.util.Date )-CEST 2018年7月31日星期二00:53:43和utcTimeOffset = +0200,表示,日期是世界标准时间+2小时。



这是遗留代码,Java 8不是一个选择。我得到的是来自第三方API的日期文本表示为 20180730131847 和utcTimeOffset = +0200 。 / p>

我想将其转换为丹麦时间。

解决方案

java.time,现代Java日期和时间API



  ZoneId danishTime = ZoneId.of(欧洲/哥本哈根); 
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern( uuuuMMddHHmmss);
DateTimeFormatter offsetFormatter = DateTimeFormatter.ofPattern( XX);

String dateTimeString = 20180730131847;
字符串offsetString = +0200;
ZoneOffset偏移量= ZoneOffset.from(offsetFormatter.parse(offsetString));
ZonedDateTime dateTime = LocalDateTime.parse(dateTimeString,dateTimeFormatter)
.atOffset(offset)
.atZoneSameInstant(danishTime);
System.out.println(丹麦时间: + dateTime);

此代码的输出为:


丹麦时间:2018-07-30T13:18:47 + 02:00 [欧洲/哥本哈根]


丹麦使用的时区是欧洲/哥本哈根。法罗群岛和格陵兰使用其他时区,并且与丹麦同居一个国家(rigsfællesskab),但它们不在丹麦境内,因此在要求丹麦时间时可以忽略不计。由于丹麦的夏令时与您的示例偏移量+0200一致,因此在这种情况下,我们得到的时间与我们输入的时间相同。例如,在冬季,由于丹麦的标准时间为偏移量,所以情况并非如此+0100。



不能使用Java 8



没什么大问题。




  • 在Java 8和更高版本以及新的Android设备上(有人告诉我,从API级别26开始)

  • 在Java 6和7中获得了ThreeTen Backport,这是新类的backport(JSR 310的ThreeTen,是最早描述现代API的地方)。在下面的链接上。

  • 在旧版Android上,使用Android版本的ThreeTen Backport。叫做ThreeTenABP。确保从包 org.threeten.bp 和子包中导入日期和时间类。



在反向移植中,类位于带有子包的软件包 org.threeten.bp 中,例如 org.threeten.bp.ZoneId org.threeten.bp.format.DateTimeFormatter



链接




I have a Java Date (java.util.Date) - Tue Jul 31 00:53:43 CEST 2018 and a utcTimeOffset = +0200, which says that give, date is +2 hours from UTC.

This is a legacy code and Java 8 is not an option. What I am getting is text representation of date as 20180730131847 and utcTimeOffset = +0200 from a third-party API.

I would like to convert this to Danish time. Can someone help me how to do this please.

解决方案

java.time, the modern Java date and time API

    ZoneId danishTime = ZoneId.of("Europe/Copenhagen");
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("uuuuMMddHHmmss");
    DateTimeFormatter offsetFormatter = DateTimeFormatter.ofPattern("XX");

    String dateTimeString = "20180730131847";
    String offsetString = "+0200";
    ZoneOffset offset = ZoneOffset.from(offsetFormatter.parse(offsetString));
    ZonedDateTime dateTime = LocalDateTime.parse(dateTimeString, dateTimeFormatter)
            .atOffset(offset)
            .atZoneSameInstant(danishTime);
    System.out.println("Danish time: " + dateTime);

Output from this code is:

Danish time: 2018-07-30T13:18:47+02:00[Europe/Copenhagen]

The time zone to use for Denmark is Europe/Copenhagen. While the Faroe islands and Greenland use other time zones and are in a national community ("rigsfællesskab") with Denmark under the same queen, they are not part of Denmark proper, so can be ignored when Danish time is asked for. Since Danish summer time agrees with your example offset of +0200, in this case we get the same time out as we put in. With a date in winter, for example, this would not have been the case because Danish standard time is at offset +0100.

Java 8 is not an option

No big problem. java.time has been backported.

  • In Java 8 and later and on new Android devices (from API level 26, I’m told) the new API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310, where the modern API was first described). Link below.
  • On (older) Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. Make sure you import the date and time classes from package org.threeten.bp and subpackages.

In the backport the classes are in package org.threeten.bp with subpackages, for example org.threeten.bp.ZoneId and org.threeten.bp.format.DateTimeFormatter.

Links

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

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