简体   繁体   English

Java转换UTC / CET时间

[英]Java convert UTC / CET time

I want to convert a given date time (which is a utc date time) to the corresponding date time in CET with a proper mapping of the european summer/winter time switch (daylight saving time). 我想使用欧洲夏季/冬季时间开关(夏令时)的正确映射,将给定的日期时间(即utc日期时间)转换为CET中的相应日期时间。 I managed to to the opposite (CET to UTC) using java.time : 我使用java.time设法做到相反(从CET到UTC):

public static LocalDateTime cetToUtc(LocalDateTime timeInCet) {
    ZonedDateTime cetTimeZoned = ZonedDateTime.of(timeInCet, ZoneId.of("CET"));
    return cetTimeZoned.withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime();
}

But I fail to go the opposite way: 但我没有走相反的方向:

public static LocalDateTime utcToCet(LocalDateTime timeInUtc) {
     ZonedDateTime cetTimeZoned = ZonedDateTime.of(timeInUtc,ZoneId.of("UTC"));
     return cetTimeZoned.withZoneSameInstant(ZoneOffset.of(???)).toLocalDateTime(); // what to put here?
 }

How can I do that? 我怎样才能做到这一点?

Just use ZoneId.of("CET") 只需使用ZoneId.of(“ CET”)

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class Main {

    public static void main(String args[])
    {
        LocalDateTime date = LocalDateTime.now(ZoneId.of("CET"));
        System.out.println(date);

        LocalDateTime utcdate = cetToUtc(date);
        System.out.println(utcdate);

        LocalDateTime cetdate = utcToCet(utcdate);
        System.out.println(cetdate);
    }

    public static LocalDateTime cetToUtc(LocalDateTime timeInCet) {
        ZonedDateTime cetTimeZoned = ZonedDateTime.of(timeInCet, ZoneId.of("CET"));
        return cetTimeZoned.withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime();
    }

    public static LocalDateTime utcToCet(LocalDateTime timeInUtc) {
         ZonedDateTime utcTimeZoned = ZonedDateTime.of(timeInUtc,ZoneId.of("UTC"));
         return utcTimeZoned.withZoneSameInstant(ZoneId.of("CET")).toLocalDateTime();
     }
}

TL;DR: In both of your methods use ZoneId.of("Europe/Rome") (or your favourite city in the CET time zone) and ZoneOffset.UTC . TL; DR:在这两种方法中,请使用ZoneId.of("Europe/Rome") (或您在CET时区中最喜欢的城市)和ZoneOffset.UTC

As Jerry06 said in a comment, using ZoneId.of("CET") again works (you already used it in your first method). 正如Jerry06在评论中所说,再次使用ZoneId.of("CET") (您已经在第一种方法中使用了它)。

However, the three-letter time zone abbreviations are not recommended, and many of them are ambiguous. 但是,不建议使用三个字母的时区缩写,其中许多是含糊的。 They recommend you use one of the city time zone IDs instead, for example ZoneId.of("Europe/Rome") for CET (this will give you CEST starting yesterday). 他们建议您改用城市时区ID之一,例如,CET的ZoneId.of("Europe/Rome") (这将使CEST从昨天开始)。 Also rather than ZoneId.of("UTC") they recommend ZoneOffset.UTC . 此外,他们还建议使用ZoneOffset.UTC而不是ZoneId.of("UTC") Passing a ZoneOffset works because ZoneOffset is one of the subclasses of ZoneId . 传递一个ZoneOffset工作,因为ZoneOffset是的子类之一ZoneId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM