在Android 12中,日期格式出现了问题,可能是由于机器语言和用户语言的不一致导致的。
以下是解决这个问题的代码示例:
示例1:使用SimpleDateFormat类
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
String formattedDate = dateFormat.format(new Date());
示例2:使用DateTimeFormatter类
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
.withLocale(Locale.getDefault());
String formattedDate = formatter.format(LocalDate.now());
这些示例可以在应用中使用,使在Android 12中显示日期更加可靠。