tl;博士
String output = LocalDate.now( ZoneId.of( "America/Montreal" ) ).format( DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ).withLocale( Locale.CANADA_FRENCH ) );
详情
问题和其他答案使用旧的过时类(SimpleDateFormat、Date、Calendar 等)。
改为使用 Java 8 及更高版本中内置的 java.time 框架。见Oracle Tutorial。大部分 java.time 功能在ThreeTen-Backport 中向后移植到 Java 6 和 7,并在 ThreeTenABP 中进一步适应 Android。
LocalDate 类表示没有时间和时区的仅日期值。
要获取当前日期,您需要一个时区。对于任何给定的时刻,全球不同时区的日期。
ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( zoneId );
要获取标准 ISO 8601 格式 YYYY-MM-DD,请致电 toString。
String output = today.toString();
要生成的字符串的localize the format and content 表示日期值,请使用DateTimeFormatter 和FormatStyle 来确定长度(完整、长、中、短)。
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT );
分配一个特定的Locale,而不是你的JVM当前的默认值。
Locale locale = Locale.CANADA_FRENCH;
formatter = formatter.withLocale( locale );
String output = today.format( formatter );
就是这样,很简单。只需根据需要定义一个Locale,例如Locale.CANADA、Locale.CANADA_FRENCH、Locale.GERMANY、Locale.KOREA,或者使用各种构造函数来传递人类语言,以及可选的国家和变体。