3

Is there a way to format a localized time duration in Kotlin or Java? I have a Duration, and I want to display it in a localized, human readable string like "1 minute 30 seconds" or "1m 30s".

I did find RelativeDateTimeFormatter, but it only appears to generate time parts that are in the future or past tense like "1 minute ago" or "in 1 minute". Is there a way to configure it to generate the present tense?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
frodo2975
  • 10,340
  • 3
  • 34
  • 41
  • This is not a duplicate, none of the answers in the linked question are localized. – frodo2975 Dec 19 '22 at 21:28
  • 1
    Interesting… The Unicode [CLDR](https://en.wikipedia.org/wiki/Common_Locale_Data_Repository) ([used by modern implementations of Java](https://openjdk.org/jeps/252)) does indeed seem to offer localization data for [duration values such as *minutes*](https://unicode-org.github.io/cldr-staging/charts/42/by_type/units.duration.html). I am voting to re-open. – Basil Bourque Dec 19 '22 at 21:53
  • 1
    I suggest you add a link to documentation on this `RelativeDateTimeFormatter ` class you mention. I do not see it bundled with Java SE. – Basil Bourque Dec 19 '22 at 21:55
  • Joda seems to have had classes for formatting Period. It's odd that these didn't end up in java.time. (Of course, that doesn't deal with the localisation, but it's half of the issue). – Dawood ibn Kareem Dec 19 '22 at 22:00
  • As the other comments seem to indicate, I believe that this is not built into Java (probably not Kotlin either). – Ole V.V. Dec 20 '22 at 05:33
  • Did you check [How to format a duration in java?](https://stackoverflow.com/q/266825/10819573)? – Arvind Kumar Avinash Dec 27 '22 at 10:15

0 Answers0