ZonedDateTime to UTC with offset applied?

前端 未结 4 688
不知归路
不知归路 2020-12-31 16:31

I am using Java 8
This is what my ZonedDateTime looks like

2013-07-10T02:52:49+12:00

I get this value as <

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-31 17:04

    Is this what you want? This converts your ZonedDateTime to a LocalDateTime with a given ZoneId by converting your ZonedDateTime to an Instant before.

    LocalDateTime localDateTime = LocalDateTime.ofInstant(z1.toInstant(), ZoneOffset.UTC);
    

    Or maybe you want the users system-timezone instead of hardcoded UTC:

    LocalDateTime localDateTime = LocalDateTime.ofInstant(z1.toInstant(), ZoneId.systemDefault());
    

提交回复
热议问题