Convert ZonedDateTime to LocalDateTime at time zone

前端 未结 4 1986
傲寒
傲寒 2020-12-28 15:42

I have an object of ZonedDateTime that is constructed like this

ZonedDateTime z = ZonedDateTime.of(LocalDate.now().atTime(11, 30), ZoneOffset.UT         


        
4条回答
  •  情话喂你
    2020-12-28 15:53

    How can I convert it to LocalDateTime at time zone of Switzerland?

    You can convert the UTC ZonedDateTime into a ZonedDateTime with the time zone of Switzerland, but maintaining the same instant in time, and then get the LocalDateTime out of that, if you need to. I'd be tempted to keep it as a ZonedDateTime unless you need it as a LocalDateTime for some other reason though.

    ZonedDateTime utcZoned = ZonedDateTime.of(LocalDate.now().atTime(11, 30), ZoneOffset.UTC);
    ZoneId swissZone = ZoneId.of("Europe/Zurich");
    ZonedDateTime swissZoned = utcZoned.withZoneSameInstant(swissZone);
    LocalDateTime swissLocal = swissZoned.toLocalDateTime();
    

提交回复
热议问题