Getting ZoneId from a SimpleTimeZone

前端 未结 3 857
無奈伤痛
無奈伤痛 2020-12-12 06:36

Using Java I have a SimpleTimeZone instance with GMT offset and daylight saving time information from a legacy system.

I would like to retrieve Zo

3条回答
  •  执念已碎
    2020-12-12 07:09

    Here the solution i prefer because it's simple, but you need a Date and a TimeZone as parameters to retrievethe the ZoneId

    private ZoneId getZoneOffsetFor(final Date date, final TimeZone timeZone){
      int offsetInMillis = getOffsetInMillis(date, timeZone);
      return ZoneOffset.ofTotalSeconds( offsetInMillis / 1000 );
    }
    
    private int getOffsetInMillis(final Date date, final TimeZone timeZone){
      int offsetInMillis = timeZone.getRawOffset();
      if(timeZone.inDaylightTime(date)){
         offsetInMillis += timeZone.getDSTSavings();
      }
      return offsetInMillis;
    }
    

提交回复
热议问题