Reputation: 1600
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?
Upvotes: 2
Views: 397
Reputation: 1600
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.
Upvotes: 4
Reputation: 36
Are you missing some dependencies?
<dependency>
<groupId>net.time4j</groupId>
<artifactId>time4j-base</artifactId>
<version>5.6</version>
</dependency>
<dependency>
<groupId>net.time4j</groupId>
<artifactId>time4j-sqlxml</artifactId>
<version>5.6</version>
</dependency>
<dependency>
<groupId>net.time4j</groupId>
<artifactId>time4j-tzdata</artifactId>
<version>5.0-2020a</version>
</dependency>
In my tests, your code work fine.
val duration = Duration.of(7261, ClockUnit.SECONDS).with(Duration.STD_CLOCK_PERIOD)
val prettifiedDuration = PrettyTime.of(Locale.ENGLISH).print(duration)
println(prettifiedDuration)
Output:
2 hours, 1 minute, and 1 second
Upvotes: 1