微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

接收日期,包括时区偏移量作为字符串并使用该时区转换为不同的格式

如何解决接收日期,包括时区偏移量作为字符串并使用该时区转换为不同的格式

我从我的客户那里收到一个这样格式的字符串

yyyy-MM-dd'T'HH:mm:ss.SSSXXX:

2021-02-04T15:31:22.265-05:00
2021-02-04T15:31:22.265+00:00
2021-02-04T15:31:22.265-06:00

我需要将它们转换为新格式:

MMMM dd,yyyy - HH:mm a z (where z is the three character abbreviation for the timezone. Ex. EST)

February 02,2021 - 15:31 PM EST
February 02,2021 - 15:31 PM UTC
February 02,2021 - 15:31 PM CST

通常我会这样做:

SimpleDateFormat inDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
        SimpleDateFormat outDateFormat = new SimpleDateFormat("MMMM dd,yyyy - HH:mm a z")
        Date date = inDateFormat.parse("2021-02-04T15:31:22.265-05:00");
        String convertedTime = outDateFormat.format(date) // February 02,2021 - 15:31 PM EST

但我随后意识到它只是在打印 EST,因为那是我所在的位置。但是如果用户在另一个时区并使用它,它将成为他们的时间,但 EST 仍然会在最后。例如,如果他们发送 2021-02-04T15:31:22.265+00:00,那么它将返回 2021 年 2 月 2 日 - 美国东部时间下午 15:31,这对他们来说是过去 5 个小时。

那么我怎样才能获取一个包含偏移量的字符串,然后简单地将它转换为来自相同偏移量的不同格式的字符串?我希望有人从 UTC 发送 2021-02 -04T15:31:22.265+00:00 为 2021 年 2 月 2 日 - UTC 下午 15:31

解决方法

您可以从时区 ID 导出时区偏移量,但不能反过来,即您不能使用时区偏移量导出时区 ID;因为相同的时区偏移量可以适用于许多时区 ID,例如

以下代码绝对有效

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime zdt = ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Europe/Paris]");
        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("MMMM dd,uuuu - HH:mm a XXX");
        System.out.println(zdt.format(dtfOutput));
    }
}

输出:

December 03,2007 - 10:15 am +01:00

而下面的代码会抛出异常:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        OffsetDateTime odt = OffsetDateTime.parse("2021-02-04T15:31:22.265-05:00");
        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("MMMM dd,uuuu - HH:mm a z");
        System.out.println(odt.format(dtfOutput));
    }
}

输出:

Exception in thread "main" java.time.DateTimeException: Unable to extract ZoneId from temporal 2021-02-04T15:31:22.265-05:00
    at java.base/java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:289)
    at java.base/java.time.format.DateTimeFormatterBuilder$ZoneTextPrinterParser.format(DateTimeFormatterBuilder.java:4083)
    at java.base/java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2343)
    at java.base/java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1848)
    at java.base/java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1822)
    at java.base/java.time.OffsetDateTime.format(OffsetDateTime.java:1681)
    at Main.main(Main.java:8)

Trail: Date Time 了解有关现代日期时间 API 的更多信息。

请注意,java.util 的日期时间 API 及其格式 API SimpleDateFormat 已过时且容易出错。建议完全停止使用它们并切换到 modern date-time API

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。