如何将 ZULU 时间戳转换为欧洲/巴黎时区?

Atu*_*Rai 1 java timezone utc datetime-conversion java-time

我有一个 ZULU 时间戳,必须将其转换为巴黎时区。

ZULU 2022-11-04T06:10:08.606+00:00  --> Paris 2022-11-04T07:10:08.606+01:00
Run Code Online (Sandbox Code Playgroud)

并且必须注意 DST 例如:夏季时间 +2 小时 冬季时间 +1 小时

我编写了以下代码,该代码在本地按预期工作,但在服务器(巴黎)上部署时未按预期工作。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Locale;

public class ParisTime {

    public static void main(String[] args) throws ParseException {

        // String date = "2022-05-31T23:30:12.209+00:00";
        String date = "2022-11-04T06:10:08.606+00:00";

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ENGLISH);
        LocalDateTime dateTime = dateFormat.parse(date).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        ZonedDateTime of = ZonedDateTime.of(dateTime, ZoneId.of("Europe/Paris"));
        String hourDiff = of.toString().substring(of.toString().indexOf('+') + 1, of.toString().indexOf('+') + 3);
        String zonedDateTime = of.plusHours(Integer.valueOf(hourDiff)).toString();
        String newDatetime = zonedDateTime.substring(0, of.toString().indexOf('['));

        System.out.println(newDatetime);

        System.out.println(dateFormat.parse(newDatetime));
    }
}
Run Code Online (Sandbox Code Playgroud)

输出

2022-11-04T07:10:08.606+01:00
Fri Nov 04 07:10:08 IST 2022
Run Code Online (Sandbox Code Playgroud)

deH*_*aar 6

您可以使用 plain 直接在区域和偏移之间切换java.time,无需遗留行李\xe2\x80\xa6

\n

就是这样:

\n
    \n
  • 您的输入示例是 ISO 格式的日期时间,其与 UTC 的偏移量(0 小时和 0 分钟,因此分别是 UTC 中的祖鲁时间),这意味着您可以一次性解析它java.time.OffsetDateTime
  • \n
  • anOffsetDateTime可以转换为ZonedDateTime
  • \n
  • aZonedDateTime可以处理夏令时 (DST)
  • \n
  • 拥有一个,ZonedDateTime您可以切换它ZoneId,这将尊重 DST,但保持底层即时
  • \n
\n

请看下面的例子\xe2\x80\xa6

\n
public static void main(String[] args) {\n    // input example\n    String date = "2022-11-04T06:10:08.606+00:00";\n    // directly parse it to a java.time.OffsetDateTime\n    OffsetDateTime odt = OffsetDateTime.parse(date);\n    // make the UTC/Zulu datetime zoned\n    ZonedDateTime zdt = odt.toZonedDateTime();\n    // print it\n    System.out.println(zdt);\n    // switch the zone to the desired one\n    ZonedDateTime zdtParis = zdt.withZoneSameInstant(ZoneId.of("Europe/Paris"));\n    // print that, too\n    System.out.println(zdtParis);\n    // or print a coversion to OffsetDateTime without explicitly mentioning the zone\n    System.out.println(zdtParis.toOffsetDateTime());\n    // the same can be achieved keeping the ZonedDateTime but formatting it as OffsetDateTime\n    System.out.println(zdtParis.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));\n}\n
Run Code Online (Sandbox Code Playgroud)\n

上述代码的输出是

\n
public static void main(String[] args) {\n    // input example\n    String date = "2022-11-04T06:10:08.606+00:00";\n    // directly parse it to a java.time.OffsetDateTime\n    OffsetDateTime odt = OffsetDateTime.parse(date);\n    // make the UTC/Zulu datetime zoned\n    ZonedDateTime zdt = odt.toZonedDateTime();\n    // print it\n    System.out.println(zdt);\n    // switch the zone to the desired one\n    ZonedDateTime zdtParis = zdt.withZoneSameInstant(ZoneId.of("Europe/Paris"));\n    // print that, too\n    System.out.println(zdtParis);\n    // or print a coversion to OffsetDateTime without explicitly mentioning the zone\n    System.out.println(zdtParis.toOffsetDateTime());\n    // the same can be achieved keeping the ZonedDateTime but formatting it as OffsetDateTime\n    System.out.println(zdtParis.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));\n}\n
Run Code Online (Sandbox Code Playgroud)\n

尝试使用受 DST 影响的日期时间,您将看到 DST 受到尊重\xe2\x80\xa6\n这是使用输入值的相同代码的输出"2022-05-31T23:30:12.209+00:00"

\n
2022-11-04T06:10:08.606Z\n2022-11-04T07:10:08.606+01:00[Europe/Paris]\n2022-11-04T07:10:08.606+01:00\n2022-11-04T07:10:08.606+01:00\n
Run Code Online (Sandbox Code Playgroud)\n