【发布时间】:2020-12-14 04:55:04
【问题描述】:
每个时区都有自己的日期/时间格式。 比如说,
UK >> dd/MM/yyyy
US >> M/d/yyyy
更多信息,https://gist.github.com/mlconnor/1887156
我确信 Android 可能有办法检索默认日期格式。 任何人都可以指导什么是最简单的获取方法?
【问题讨论】:
每个时区都有自己的日期/时间格式。 比如说,
UK >> dd/MM/yyyy
US >> M/d/yyyy
更多信息,https://gist.github.com/mlconnor/1887156
我确信 Android 可能有办法检索默认日期格式。 任何人都可以指导什么是最简单的获取方法?
【问题讨论】:
根据上述输入,改进了 kotlin 中的答案并具有向后兼容性。
private fun getDefaultDatePattern(): String {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
DateTimeFormatterBuilder.getLocalizedDateTimePattern(
FormatStyle.SHORT,
null,
IsoChronology.INSTANCE,
Locale.getDefault()
)
} else {
SimpleDateFormat().toPattern()
}
}
【讨论】:
DateTimeFormatterBuilder#getLocalizedDateTimePatternimport java.text.DateFormat;
import java.time.LocalDate;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.FormatStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
for (Locale locale : DateFormat.getAvailableLocales()) {
String datePattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, null,
IsoChronology.INSTANCE, locale);
System.out.println("Locale: " + locale.getLanguage() + ", Date Format: " + datePattern + ", Today: "
+ LocalDate.now().format(DateTimeFormatter.ofPattern(datePattern, locale)));
}
}
}
输出:
Locale: , Date Format: y-MM-dd, Today: 2020-08-25
Locale: nn, Date Format: dd.MM.y, Today: 25.08.2020
Locale: ar, Date Format: d/M/y, Today: 25/8/2020
...
...
...
Locale: ccp, Date Format: d/M/yy, Today: 25/8/20
Locale: br, Date Format: dd/MM/y, Today: 25/08/2020
【讨论】: