I'm using ThreeTen-Backport library for my Android project (because java.time is not yet implemented in android development).
When I write LocalDate today=LocalDate.now();
or LocalTime time=LocalTime.now();
I get the following exception:
Caused by: org.threeten.bp.zone.ZoneRulesException:
No time-zone data files registered
at org.threeten.bp.zone.ZoneRulesProvider.getProvider(ZoneRulesProvider.java:176)
at org.threeten.bp.zone.ZoneRulesProvider.getRules(ZoneRulesProvider.java:133)
at org.threeten.bp.ZoneRegion.ofId(ZoneRegion.java:143)
at org.threeten.bp.ZoneId.of(ZoneId.java:357)
at org.threeten.bp.ZoneId.of(ZoneId.java:285)
at org.threeten.bp.ZoneId.systemDefault(ZoneId.java:244)
at org.threeten.bp.Clock.systemDefaultZone(Clock.java:137)
at org.threeten.bp.LocalDate.now(LocalDate.java:165)
The same line of code works well in another java project I have, which uses the native java.time library.
I searched for a possible solution but couldn't find anything useful: one solution suggested I need to use another jar that includes the time-zone rules and other suggested that there might be two or more ThreeTenBP-libraries inside the classpath.
Those cases don't match my case.
Inside the build.gradle
file, at the dependencies section, I've tried few configurations:
compile 'com.jakewharton.threetenabp:threetenabp:1.0.3'
compile 'org.threeten:threetenbp:1.0.3'
compile 'org.threeten:threetenbp:1.3.1'
compile 'org.threeten:threetenbp:1.3.2'
I don't know what is wrong with that line of code and how to fix it.
The LocalDate.now()
and LocalTime.now()
methods should work without specifying a time zone.
For Android project you should use
implementation 'com.jakewharton.threetenabp:threetenabp:1.0.3'
Make sure you call AndroidThreeTen.init(this);
before using the classes from the library. This will read the time zones data (included in the library). You can initialize the library in your Application
class in the onCreate
method just like it is recommended in the README.
Instead of initialization of the library, you can try this:
LocalDateEx.kt
object LocalDateEx {
/**an alternative of LocalDate.now(), as it requires initialization using AndroidThreeTen.init(context), which takes a bit time (loads a file)*/
@JvmStatic
fun getNow(): LocalDate = Calendar.getInstance().toLocalDate()
}
fun Calendar.toLocalDate(): LocalDate = LocalDate.of(get(Calendar.YEAR), get(Calendar.MONTH) + 1, get(Calendar.DAY_OF_MONTH))
LocalTimeEx.kt
object LocalTimeEx {
/**an alternative of LocalDateTime.now(), as it requires initialization using AndroidThreeTen.init(context), which takes a bit time (loads a file)*/
@JvmStatic
fun getNow(): LocalTime = Calendar.getInstance().toLocalTime()
}
fun Calendar.toLocalTime(): LocalTime = LocalTime.of(get(Calendar.HOUR_OF_DAY), get(Calendar.MINUTE), get(Calendar.SECOND), get(Calendar.MILLISECOND) * 1000000)
LocalDateTimeEx.kt
object LocalDateTimeEx {
/**an alternative of LocalDateTime.now(), as it requires initialization using AndroidThreeTen.init(context), which takes a bit time (loads a file)*/
@JvmStatic
fun getNow(): LocalDateTime = Calendar.getInstance().toLocalDateTime()
}
private fun Calendar.toLocalDateTime(): LocalDateTime = LocalDateTime.of(get(Calendar.YEAR), get(Calendar.MONTH) + 1, get(Calendar.DAY_OF_MONTH), get(Calendar.HOUR_OF_DAY), get(Calendar.MINUTE), get(Calendar.SECOND),
get(Calendar.MILLISECOND) * 1000000)
Usage:
val today=LocalDateEx.getNow()
val today2=LocalTimeEx.getNow()
val today3=LocalDateTimeEx.getNow()
For me, it wasn't working on signed release build with proguard enabled. Check your proguard rules if they contain
-keep class org.threeten.bp.zone.*
unless, add it. It helped for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With