I am receiving a text which is in seconds (e.g. 7261). I need to convert it to this format: 2h, 1m, 1s
I am using time4j library. According to their Github page, I am doing something like this:
val duration = Duration.of(7261, ClockUnit.SECONDS).with(Duration.STD_CLOCK_PERIOD)
val prettifiedDuration = PrettyTime.of(Locale.ENGLISH).print(duration)
However, I get this error:
06-22 11:50:52.133 12161 12244 E AndroidRuntime: java.lang.ExceptionInInitializerError
06-22 11:50:52.133 12161 12244 E AndroidRuntime: at net.time4j.base.ResourceLoader.getInstance(ResourceLoader.java:167)
06-22 11:50:52.133 12161 12244 E AndroidRuntime: at net.time4j.PrettyTime.<clinit>(PrettyTime.java:95)
06-22 11:50:52.133 12161 12244 E AndroidRuntime: at net.time4j.PrettyTime.of(PrettyTime.java:198)
Any idea? or even, better solution?
So, I realized there is a separate library for Android projects: https://github.com/MenoData/Time4A
For Gradle, add this dependecy:
implementation("net.time4j:time4j-android:4.6-2020a")
And in your Application class, initialize the library:
class MyApplication : DaggerApplication() {
override fun onCreate() {
super.onCreate()
ApplicationStarter.initialize(this, true)
}
}
Then you can use it as described in the question.