Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 如何根据设备区域设置,使用时间缩写格式化LocalTime?_Kotlin_Java 8_Localtime_Time Format - Fatal编程技术网

Kotlin 如何根据设备区域设置,使用时间缩写格式化LocalTime?

Kotlin 如何根据设备区域设置,使用时间缩写格式化LocalTime?,kotlin,java-8,localtime,time-format,Kotlin,Java 8,Localtime,Time Format,根据设备显示LocalTime值的最佳方式是什么?美国是唯一一个专门使用AM/PM时间缩写的国家,但由于某些原因,每当我的设备区域设置设置为美国时,缩写不会出现 美国 上午2:30/下午2:30 世界其他地方 02:30/14:30 当前代码 myTV.text=LocalTime.of(2,30)+“”+LocalTime.of(14,30) 当前结果 2:30 14:30您需要明确设置时间格式。使用校准时间的日期时间格式 DateTimeFormatter localizedTime

根据设备显示
LocalTime
值的最佳方式是什么?美国是唯一一个专门使用AM/PM时间缩写的国家,但由于某些原因,每当我的设备区域设置设置为美国时,缩写不会出现

美国

上午2:30/下午2:30

世界其他地方

02:30/14:30

当前代码

myTV.text=LocalTime.of(2,30)+“”+LocalTime.of(14,30)

当前结果


2:30 14:30

您需要明确设置时间格式。使用校准时间的日期时间格式

    DateTimeFormatter localizedTimeFormatter
            = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
    LocalTime begin = LocalTime.of(2, 30);
    LocalTime end = LocalTime.of(14, 30);

    String text = begin.format(localizedTimeFormatter) + ' ' + end.format(localizedTimeFormatter);
    System.out.println(text);
很抱歉,我只能写Java代码。当我在桌面Java 9上运行代码时,默认语言环境设置为US,输出为:

上午2:30下午2:30

与其他语言环境的输出:

  • 加拿大:
    凌晨2:30下午2:30
  • 英国或法国:
    02:30 14:30

@OleV.V。这个答案只显示我是否想一直使用AM/PM,而我没有。