Using Joda-Time to get UTC offset for a given date and timezone

前端 未结 5 796
余生分开走
余生分开走 2021-01-01 19:03

I have dates in the format 20Jan2013, 08Aug2012 etc, with their own specific timezones. So for example, 20Jan2013 might have a timezone ID of Australia/Melbourne, and 08Aug2

5条回答
  •  执念已碎
    2021-01-01 19:52

    In order for Joda to give the correct offset, you must provide a datetime instant.Without a datetime instant, it is impossible to calculate the offset since we have different offsets(daylight savings). This is how I would use Joda to get offset in + HH:mm format :

        int offsetInMillis = DateTimeZone.forID(zoneId).getOffset(new DateTime().getMillis());
        String offset = String.format("%02d:%02d", Math.abs(offsetInMillis / 3600000),
                Math.abs((offsetInMillis / 60000) % 60));
        offset = (offsetInMillis >= 0 ? "+" : "-") + offset; 
    

提交回复
热议问题