【问题标题】:Java8 time library does not interpret BST timezone correctlyJava8 时间库无法正确解释 BST 时区
【发布时间】:2019-10-29 08:33:19
【问题描述】:

这不是另一个时区问题的重复,因为我没有选择时区格式的奢侈。我正在迁移我的 java 代码以使用新的 java8 时间库,但我发现 DateTimeFormatter 没有正确解释时区“BST”(英国夏令时)。它没有将其设为 UTC+0100,而是将其转换为太平洋/布干维尔时区。任何人都知道如何在不回到旧的 SimpleDateFormat 或使用明确设置的时区的情况下解决此问题,因为我的代码需要在世界上的多个区域运行?此时间戳格式是通过查询另一个系统获得的,因此我无法更改它。似乎 SimpleDateFormat 可以正确识别时区。我的测试代码如下:

public static void main(String[] args) throws ParseException {
    String sTime = "Fri Jun 07 14:07:07 BST 2019";
    DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("EEE MMM dd kk:mm:ss z yyyy");
    ZonedDateTime zd = ZonedDateTime.parse(sTime, FORMATTER);
    System.out.println("The time zone: " + zd.getZone());
    FileTime ftReturn = FileTime.from(zd.toEpochSecond(), TimeUnit.SECONDS);
    System.out.println("File time is: " + ftReturn);

    DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH);
    Date dtDD = df.parse(sTime);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(dtDD);
    FileTime ftReturn1 = FileTime.fromMillis(calendar.getTimeInMillis());
    System.out.println("File time1 is:  " + ftReturn1);
}

测试结果:
时区:太平洋/布干维尔
文件时间为:2019-06-07T03:07:07Z
文件时间1为:2019-06-07T13:07:07Z

【问题讨论】:

  • 不是重复的。另一个问题是关于ZoneId.SHORT_IDS,而这个问题是关于DateTimeFormatter。他们不会以相同的方式解释 BST,说服每个人将其解释为英国夏令时的方式也大不相同。
  • 您确定要在格式模式字符串中使用从 01 到 24 小时的 kk 吗? HH 小时从 00 到 23 会更传统。

标签: java java-8 datetime-format zoneddatetime


【解决方案1】:

首先,您正在尝试解决一项无法解决的任务。三个字母的时区缩写是模棱两可的,我想更多的是。因此,如果您解决了欧洲/伦敦的问题,当您遇到 EST、IST、CST、CDT、PST、WST 等等时,您将再次遇到它。或者当您遇到 BST 表示巴西夏令时间、孟加拉国标准时间或布干维尔标准时间的字符串时。还有更多的解释存在。取而代之的是一个明确的字符串,比如一个带有 UTC 偏移量而不是时区缩写的字符串,最好是一个 ISO 8601 格式的字符串,比如2019-06-07T14:07:07+01:00。

但是,如果您确定 BST 在您的世界中始终意味着英国夏令时,那么短视的解决方案可能是告诉 DateTimeFormatter 您更喜欢哪个时区:

    String sTime = "Fri Jun 07 14:07:07 BST 2019";
    ZoneId preferredZone = ZoneId.of("Europe/London");
    DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
            .appendPattern("EEE MMM dd HH:mm:ss ")
            .appendZoneText(TextStyle.SHORT, Collections.singleton(preferredZone))
            .appendPattern(" yyyy")
            .toFormatter(Locale.ROOT);
    ZonedDateTime zd = ZonedDateTime.parse(sTime, FORMATTER);
    System.out.println("The time zone: " + zd.getZone());
    FileTime ftReturn = FileTime.from(zd.toEpochSecond(), TimeUnit.SECONDS);
    System.out.println("File time is: " + ftReturn);

输出是:

The time zone: Europe/London
File time is: 2019-06-07T13:07:07Z

链接

【讨论】:

    【解决方案2】:

    根据https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html#SHORT_IDS:

    BST - 亚洲/达卡

    所以我猜你不应该使用那个缩写。

    编辑:刚刚找到这个question,它回答了它。

    所以不要使用孟加拉国标准时间 ;) 而是使用 ZoneId.of("Europe/London")

    【讨论】:

    • 正如我在帖子中所说,我无法选择格式。我查询了一个系统,这是它返回的格式,采用 3 个字母的时区缩写。我只是想知道在没有明确设置时区的情况下处理这个问题的最优雅的方法是什么。我的代码需要在全球多个时区运行。谢谢!
    • @LibbyShen 在解析前查找“BST”并将其替换为“Europe/London”。通知 API 的所有者他们的时间戳不明确。
    • 您建议在解析之前将“BST”替换为“Europe/London”。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 2020-04-02
    • 2022-07-08
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    相关资源
    最近更新 更多