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

前端 未结 7 1237
礼貌的吻别
礼貌的吻别 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:09

    tl;dr

    ZonedDateTime.now( 
        ZoneId.of( "America/Montreal" ) 
    )
    

    …of current default time zone…

    ZonedDateTime.now( 
        ZoneId.systemDefault() 
    )
    

    Details

    The Answer by Stanislav Bshkyrtsev correctly and directly answers your Question.

    But, there are larger issues involved, as suggested in the Answer by Jon Skeet.

    LocalDateTime

    I don't find the way to convert epoch second to LocalDateTime

    LocalDateTime purposely has no concept of time zone or offset-from-UTC. Not likely what you want. The Local… means any locality, not any one particular locality. This class does not represent a moment, only potential moments along a range of about 26-27 hours (the range of time zones around the globe).

    Instant

    No need to start with epoch seconds if you are trying to get current time. Get the current Instant. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

    Instant instant = Instant.now();
    

    Inside of that Instant is a count of nanoseconds-from-epoch. But we do not really care.

    ZonedDateTime

    If you want to see that moment through the lens of a particular region’s wall-clock time, apply a ZoneId to get a ZonedDateTime.

    ZoneId z = ZoneId.of( "Europe/Paris" );
    ZonedDateTime zdt = instant.atZone( z );
    

    As a shortcut, you can do directly to the ZonedDateTime.

    ZonedDateTime zdt = ZonedDateTime.now( z );  
    

    A ZonedDateTime has an Instant within it. Call zdt.toInstant() to get the same moment in time as a basic value in UTC. Same number of nanoseconds-since-epoch either way, as a ZonedDateTime or as a Instant.

    Seconds-since-epoch given

    If you are given a count of seconds-since-epoch, and the epoch is the first moment of 1970 in UTC (1970-01-01T00:00:00Z), then feed that number to Instant.

    long secondsSinceEpoch = 1_484_063_246L ;
    Instant instant = Instant.ofEpochSecond( secondsSinceEpoch ) ;
    


    About java.time

    The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

    You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

    Where to obtain the java.time classes?

    • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
    • Java SE 6 and Java SE 7
      • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • Later versions of Android bundle implementations of the java.time classes.
      • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

    The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

提交回复
热议问题