Joda parse ISO8601 date in GMT timezone

前端 未结 3 1024
走了就别回头了
走了就别回头了 2021-02-19 02:30

I have a ISO 8601 date, lets say: 2012-01-19T19:00-05:00

My machine timezone is GMT+1

I\'m trying to use joda to parse this and convert

3条回答
  •  心在旅途
    2021-02-19 03:24

    java.time

    The Joda-Time team has told us to migrate to the java.time framework built into Java 8 and later. The java.time framework is defined by JSR 310. Much of the java.time functionality has been back-ported to Java 6 & 7 and further adapted for Android.

    Offset

    The java.time classes include OffsetDateTime to represent a moment on the timeline with an offset-from-UTC but not a full time zone.

    The java.time classes use the standard ISO 8601 formats by default when parsing or generating strings. So no need to define a formatting pattern.

    String input = "2012-01-19T19:00-05:00";
    OffsetDateTime odt = OffsetDateTime.parse( input );
    

    Time Zone

    A time zone is an offset plus rules for handling anomalies such as Daylight Saving Time (DST). A proper time zone name uses a continent/region format. You can assign a time zone (ZoneId) to an OffsetDateTime to get a ZonedDateTime.

    ZoneId zoneId = ZoneId.of( "America/Montreal" );
    ZonedDateTime zdt = odt.atZoneSameInstant( zoneId );
    

提交回复
热议问题