Насколько я нашел, самый простой способ - это вычислить смещение по GMT для каждого часового пояса и затем суммировать их:
package dates;
import java.time.ZoneId
import java.util.TimeZone
import java.util.concurrent.TimeUnit
/**
* Returns timezone GMT delta offset for given point of time.
*/
def timezoneOffset(millis: Long, tz: ZoneId): Int = {
TimeUnit.MILLISECONDS.toHours(TimeZone.getTimeZone(tz).getOffset(millis)).toInt
}
/**
* Returns offset required to move `millis` from `baseTz` to `targetTz`:
*
* @example
* PDT(Los_Angeles) is -7 from GMT
* EDT(Paris) is +2 from GMT
*
* timezoneOffsetBetween(millisInPdt, PDT, EDT) == +9
* timezoneOffsetBetween(millisInEdt, EDT, PDT) == -9
*/
def timezoneOffsetBetween(millis: Long, baseTz: ZoneId, targetTz: ZoneId): Int = {
-(timezoneOffset(millis, baseTz) - timezoneOffset(millis, targetTz))
}
Вот тестовые случаи:
package dates;
import java.time.{ZoneId, ZonedDateTime}
import org.scalatest.{FreeSpec, Matchers}
class DateSpec extends FreeSpec with Matchers {
val laTz = ZoneId.of("America/Los_Angeles")
val gmtTz = ZoneId.of("GMT")
val sydneyTz = ZoneId.of("Australia/Sydney")
val parisTz = ZoneId.of("Europe/Paris")
"timezoneOffset should be" - {
"-7 for PDT millis" in {
val pdtMillis = ZonedDateTime.of(2018, 9, 10, 0, 0, 0, 0, laTz).toInstant.toEpochMilli
dates.timezoneOffset(pdtMillis, laTz) shouldEqual -7
}
"-8 for PST millis" in {
val pstMillis = ZonedDateTime.of(2017, 11, 6, 0, 0, 0, 0, laTz).toInstant.toEpochMilli
dates.timezoneOffset(pstMillis, laTz) shouldEqual -8
}
"0 for GMT millis" in {
val gmtMillis = ZonedDateTime.of(2017, 11, 6, 0, 0, 0, 0, gmtTz).toInstant.toEpochMilli
dates.timezoneOffset(gmtMillis, gmtTz) shouldEqual 0
}
"10 for AEST millis" in {
val aestMillis = ZonedDateTime.of(2018, 9, 10, 0, 0, 0, 0, sydneyTz).toInstant.toEpochMilli
dates.timezoneOffset(aestMillis, sydneyTz) shouldEqual 10
}
"11 for AEDT millis" in {
val aedtMillis = ZonedDateTime.of(2017, 10, 10, 0, 0, 0, 0, sydneyTz).toInstant.toEpochMilli
dates.timezoneOffset(aedtMillis, sydneyTz) shouldEqual 11
}
}
"timezoneOffsetBetween should be" - {
"17 between PDT and AEST millis" in {
val pdtMillis = ZonedDateTime.of(2018, 9, 10, 0, 0, 0, 0, laTz).toInstant.toEpochMilli
dates.timezoneOffsetBetween(pdtMillis, laTz, sydneyTz) shouldEqual 17
}
"18 between PDT and AEDT millis" in {
val pdtMillis = ZonedDateTime.of(2017, 10, 10, 0, 0, 0, 0, laTz).toInstant.toEpochMilli
dates.timezoneOffsetBetween(pdtMillis, laTz, sydneyTz) shouldEqual 18
}
"19 between PST and AEDT millis" in {
val pstMillis = ZonedDateTime.of(2017, 11, 6, 0, 0, 0, 0, laTz).toInstant.toEpochMilli
dates.timezoneOffsetBetween(pstMillis, laTz, sydneyTz) shouldEqual 19
}
"-19 between AEDT and PST millis" in {
val aedtMillis = ZonedDateTime.of(2017, 11, 6, 0, 0, 0, 0, sydneyTz).toInstant.toEpochMilli
dates.timezoneOffsetBetween(aedtMillis, sydneyTz, laTz) shouldEqual -19
}
"0 between PDT and PDT" in {
val pdtMillis = ZonedDateTime.of(2018, 9, 10, 0, 0, 0, 0, laTz).toInstant.toEpochMilli
dates.timezoneOffsetBetween(pdtMillis, laTz, laTz) shouldEqual 0
}
"8 between EDT and AEST" in {
val edtMillis = ZonedDateTime.of(2018, 9, 10, 0, 0, 0, 0, parisTz).toInstant.toEpochMilli
dates.timezoneOffsetBetween(edtMillis, parisTz, sydneyTz) shouldEqual 8
}
"-8 between AEST and EDT" in {
val aestMillis = ZonedDateTime.of(2018, 9, 10, 0, 0, 0, 0, sydneyTz).toInstant.toEpochMilli
dates.timezoneOffsetBetween(aestMillis, sydneyTz, parisTz) shouldEqual -8
}
}
}