0

I want to format a certain time/date,(HH:mm) to a specific locale type. For example, I have 12:05 and I need it to be - in french 12h05 - in dutch 12u05

Is this possible without checking the current locale and adjust the result?

fun String.hourFormat() : String {
       val time = LocalTime.parse (this, DateTimeFormatter.ISO_DATE_TIME)
       val result = DateTimeFormatter.ofPattern("HH:mm").withLocale(LocaleManager.currentLocale())
       return result
     } 

Does there exist something already implemented to format the time based on locale? The current approach for me is to manually check the locale :

   fun String.hourFormat() : String {
....
val localePattern = when (LocaleManager.currentLocale().name){
                       "FR" -> "HH'h'mm"
                       "NL" -> "HH'u'mm" 
                   }
    DateTimeFormatter.ofPattern(localePattern)

}
lexraid
  • 95
  • 2
  • 3
  • 11
  • 1
    Have you seen: https://stackoverflow.com/a/39992733/295004 – Morrison Chang May 17 '23 at 14:31
  • [`DateTimeFormatter.ofLocalizedTime`](https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/time/format/DateTimeFormatter.html#ofLocalizedTime(java.time.format.FormatStyle)) – Basil Bourque May 17 '23 at 14:50

0 Answers0