Java 8: how to get ZoneId from ZoneOffset

I read the API for the classZoneId . It states that there are three ID clues:

  • derived from ZoneOffset
  • offset style identifiers with some prefix. Examples:

    ZoneId.of("GMT+2");
    ZoneId.of("UTC");
    ZoneId.of("UT+01:00");
    
  • area based. Examples:

    ZoneId.of("Asia/Aden");
    ZoneId.of("Etc/GMT+9");
    ZoneId.of("Asia/Aqtau");
    

But what is the correct syntax for the first kind? The documentation says that

[ID from ZoneOffset] consists of "Z" and identifiers starting with "+" or "-".

What combination of String objects ZoneOffsetshould I use to create the ZoneIdfirst kind?

+4
source share
2 answers

There are actually two questions that need to be answered here.

1) ?

:

    ZoneId z;
    z = ZoneId.of("Z"); //for UTC
    z = ZoneId.of("+02:00"); 
    z = ZoneId.of("-02:00"); 

  • Z - UTC
  • +
  • +
  • + :
  • -hh:
  • +
  • -hhmm
  • + : :
  • -hh: :
  • +
  • -hhmmss

,

'Z' , '+' '-'

, Z ( - Z+02:00). , .

2) [] String ZoneOffset?

, ZoneOffset:

    ZoneId z;
    z = ZoneId.of("+02:00"); 
    z = ZoneId.of(ZoneOffset.of("+02:00").getId());
+9

"UTC" "ZoneOffset", ZoneId

ZoneId z = ZoneId.ofOffset("UTC",ZoneOffset.UTC);

http://www.java2s.com/Tutorials/Java/java.time/ZoneId/2180__ZoneId.ofOffset_String_prefix_ZoneOffset_offset_.htm

0

Source: https://habr.com/ru/post/1610150/


All Articles