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