Is there any way to convert ZoneId to ZoneOffset in java 8?

前端 未结 7 1208
礼貌的吻别
礼貌的吻别 2020-12-13 05:26

I have an epoch second and a zoneId,by method1.It can be convert to LocalDateTime with system default zoneId,but I don\'t find the way to convert epoch second to LocalDateTi

相关标签:
7条回答
  • 2020-12-13 06:13

    I have an epoch second and a zoneId. Is there any way to convert ZoneId to ZoneOffset in java 8?

    1. Get ZonedDateTime from epoch second and Zone Id
    2. Get ZoneOffset from ZonedDateTime

    Demo:

    import java.time.Instant;
    import java.time.ZoneId;
    import java.time.ZoneOffset;
    import java.time.ZonedDateTime;
    
    public class Main {
        public static void main(String[] args) {
            // Get ZonedDateTime from epoch second and Zone Id
            ZonedDateTime zdt = Instant.ofEpochSecond(1597615462L).atZone(ZoneId.of("Europe/London"));
    
            // Get ZoneOffset from ZonedDateTime
            ZoneOffset offset = zdt.getOffset();
    
            System.out.println(offset);
        }
    }
    

    Output:

    +01:00
    
    0 讨论(0)
提交回复
热议问题