Parse and retrieve timezone offset from date-time

后端 未结 4 412
深忆病人
深忆病人 2020-12-22 05:37

Date format: \"yyyy-MM-dd\'T\'HH:mm:ss.SSSZ\"

Input date: \"2017-09-18T03:08:20.888+0200\"

Problem: I need retrieve timezon

4条回答
  •  醉话见心
    2020-12-22 06:29

    tl;dr

    TimeZone.getTimeZone(       // Convert from modern java.time type (`ZoneOffset`/`ZoneId`) to legacy type (`TimeZone`)
        OffsetDateTime.parse (  // Instantiate a `OffsetDateTime`, a moment on the timeline.
            "2017-09-18T03:08:20.888+0200" ,
            DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ss.SSSX" )
        ).getOffset()           // Extract a `ZoneOffset`, which is a subclass of `ZoneId`.
    )
    

    Convert directly from modern ZoneOffset to legacy TimeZone

    The code seen here is similar to Answers by Yan Khonski, but using the variation of TimeZone.getTimeZone that directly converts from the modern java.time classes (ZoneOffset & ZoneID) to the legacy TimeZone class.

    While there is no difference in the end result, this approach uses a an explicit conversion method. This is one of many new methods added to the old date-time classes for converting to/from java.time objects.

    Using such a conversion method makes your code more self-documenting. Also makes more clear your awareness that you are consciously moving between the modern & legacy classes.

    String input = "2017-09-18T03:08:20.888+0200";
    DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ss.SSSX" );
    
    OffsetDateTime odt = OffsetDateTime.parse( input , f );  // Parse input.
    ZoneOffset offset = odt.getOffset( );                    // Interrogate for the `ZoneOffset` object representing this moment’s offset-from-UTC (a number of hours/minutes/seconds).
    
    TimeZone tz = TimeZone.getTimeZone( offset );            // Convert from modern java.time object (a `ZoneOffset`/`ZoneId`) to the legacy class `TimeZone`.
    

    Dump to console.

    System.out.println( "odt.toString(): " + odt );
    System.out.println( "offset.toString(): " + offset );
    System.out.println( "tz.toString(): " + tz );
    

    odt.toString(): 2017-09-18T03:08:20.888+02:00

    offset.toString(): +02:00

    tz.toString(): sun.util.calendar.ZoneInfo[id="GMT+02:00",offset=7200000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]


    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.

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

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

    Where to obtain the java.time classes?

    • Java SE 8, Java SE 9, and later
      • Built-in.
      • 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
      • Much 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, 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.

提交回复
热议问题