2

I am struggling to find a question which specifically asks this, so I apologise if I have duplicated.

What I want is to format integers to strings whilst appending localised strings for time durations. I would like to know if there is any way in which to do this in Android without having to translate everything myself.

Something like this:

format(locale = Locale.UK, formatStyle = FormatStyle.SHORT, hours = 2, minutes = 35) -> Output expected 2hrs 35mins
format(locale = Locale.UK, formatStyle = FormatStyle.NORMAL, hours = 2, minutes = 35) -> Output expected 2hours 35minutes

format(locale = Locale.FRA, formatStyle = FormatStyle.SHORT, hours = 2, minutes = 35) -> Output expected 2h 35m
format(locale = Locale.FRA, formatStyle = FormatStyle.NORMAL, hours = 2, minutes = 35) -> Output expected 2heures 35minutes

I would like localisation of the string "hours" and "minutes" to be done for me, much like how RelativeDateTimeFormatter does "x hours ago" when you pass in some Locale, but I cannot find this.

Note, I know how to do the "maths" of it so using Duration or something to deal with numbers isn't what I need. Also, I do not want to use my own translations so just using R.string.hours and defining that across my supported locales isn't the solution I'm looking for.

Achieving this goal along with flexibility of pluralisation would also be amazing but isn't a deal breaker.

e.g. hour/hours, hr/hrs, h

adrem7
  • 388
  • 1
  • 3
  • 14
  • Asking for library recommendations is off-topic for SO. – Tenfour04 Feb 28 '22 at 12:59
  • In this question the second most popular answer recommends using DateUtils: https://stackoverflow.com/questions/6118922/convert-seconds-value-to-hours-minutes-seconds In this one, three ways of doing something are suggested in the selected answer, DateTimeFormatter, SimpleDateFormatter and ThreeTenABP. https://stackoverflow.com/questions/53781154/kotlin-android-java-string-datetime-format-api21 I don't need a library, I need a solution to my problem. Please feel free to provide either... In the meantime I've edited my question to remove specifying the solution needs to be a library. – adrem7 Feb 28 '22 at 19:52
  • I don’t know specifically how to do it. I was just warning you so you don’t get downvoted. – Tenfour04 Feb 28 '22 at 20:07
  • I would use the LocalDateTime method of doing it from the second one you linked. ThreeTenABP is rendered obsolete now that there is Java 8 desugaring. – Tenfour04 Feb 28 '22 at 20:19

0 Answers0