首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将祖鲁时间戳转换为欧洲/巴黎时区?

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

Stack Overflow用户
提问于 2022-11-04 11:03:03
回答 2查看 105关注 0票数 1

我有一个祖鲁时间戳,我必须转换成巴黎时区。

代码语言:javascript
运行
复制
ZULU 2022-11-04T06:10:08.606+00:00  --> Paris 2022-11-04T07:10:08.606+01:00

并必须照顾DST,例如:夏季时间小时+2小时冬季时间小时+1小时

我已经编写了下面的代码,它在本地按预期工作,但在服务器(巴黎)上部署时不像预期的那样工作。

代码语言:javascript
运行
复制
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));
    }
}

输出

代码语言:javascript
运行
复制
2022-11-04T07:10:08.606+01:00
Fri Nov 04 07:10:08 IST 2022
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-11-04 11:43:05

您可以使用普通的java.time直接在区域和偏移之间切换,没有必要的遗留行李…。

下面是操作步骤:

您的输入示例是一个ISO格式的日期时间,它与UTC的偏移量(0小时和0分钟,因此在UTC中分别是祖鲁时间),这意味着您可以一次将其解析到java.time.OffsetDateTime

  • an OffsetDateTime,可以转换为ZonedDateTime

  • a ZonedDateTime可以处理夏令时(DST)

  • ,有一个ZonedDateTime可以切换其ZoneId,这将尊重DST,但保留底层的即时<>f 216。

请参见下面的示例…

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

以上代码的输出如下

代码语言:javascript
运行
复制
2022-11-04T06:10:08.606Z
2022-11-04T07:10:08.606+01:00[Europe/Paris]
2022-11-04T07:10:08.606+01:00
2022-11-04T07:10:08.606+01:00

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

代码语言:javascript
运行
复制
2022-05-31T23:30:12.209Z
2022-06-01T01:30:12.209+02:00[Europe/Paris]
2022-06-01T01:30:12.209+02:00
2022-06-01T01:30:12.209+02:00
票数 6
EN

Stack Overflow用户

发布于 2022-11-04 12:39:38

您的字符串已经采用了类parse中的静态方法java.time.Instant所识别的格式。然后,您可以将该瞬间转换为巴黎时区中的ZonedDateTime。两行代码:

代码语言:javascript
运行
复制
String date = "2022-11-04T06:10:08.606+00:00";
java.time.Instant inst = java.time.Instant.parse(date);
java.time.ZonedDateTime zdt = inst.atZone(java.time.ZoneId.of("Europe/Paris"));

它还将考虑到夏季时间和非夏季时间的变化。

代码语言:javascript
运行
复制
ZULU : 2022-11-04T06:10:08.606Z
Paris: 2022-11-04T07:10:08.606+01:00[Europe/Paris]
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74316021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档