I'm migrating an old code base away from the threeten backport library to use java.time instead. There is a test method that no longer works, but I don't know how to fix it.
Here is the class before the changes. This compiles correctly:
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import org.junit.Before
import org.junit.Rule
import org.threeten.bp.ZoneId
import org.threeten.bp.zone.TzdbZoneRulesProvider
import org.threeten.bp.zone.ZoneRulesProvider
open class BaseAvailabilityTest {
@get:Rule
val rule = InstantTaskExecutorRule()
@Before
open fun setUp() {
if (ZoneRulesProvider.getAvailableZoneIds().isEmpty()) {
val stream = this.javaClass.classLoader?.getResourceAsStream("TZDB.dat")
stream.use(::TzdbZoneRulesProvider).apply {
ZoneRulesProvider.registerProvider(this)
}
}
zoneId = ZoneId.of("America/Los_Angeles")
}
}
But when I switch the threeten imports over to java.time like this:
import java.time.ZoneId
import java.time.zone.TzdbZoneRulesProvider
import java.time.zone.ZoneRulesProvider
then Android Studio shows an error:
Type mismatch
Required: (TypeVariable(T))->TypeVariable(R)
Found: KFunction0<TzdbZoneRulesProvider>
I can't find anything that addresses this issue. Can anyone help me understand what is going on here?
You don't need to register the tzdb provider explicitly. That is the default provider for the JVM, as documented in the JavaDocs.
In fact,
TzdbZoneRulesProvideris a package-private class injava.time.zone, so you cannot access it anyway.This essentially means that you can remove this
ifentirely:If your tests depend on a particular version of tzdb (like in very rare situations such as this post), you can use the timezone updater tool to change the tzdb version in your JDK. An example is described here.