Localdate:格式与语言环境
Localdate: format with locale
我正在尝试格式化 LoacalDate,但没有找到任何信息。我需要格式化为另一种语言。案例只是我想用西班牙语获得一年中的月份。我正在尝试使用:
Locale locale = new Locale("es", "ES");
但我没有找到 SimpleDateFormat 或类似于 LocalDate 格式的格式。
LocalDate.now().getMonth();
谁能帮帮我?
抱歉,我使用了 Java 的较旧(并且仍然更常用)的日期时间 类,正如您所说的 SimpleDateFormat 是较旧 API 的一部分。
当您使用 java.time.LocalDate
时,您必须使用的格式化程序是 java.time.format.DateTimeFormatter
:
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM", aLocale);
final String month = LocalDate.now().format(formatter);
tl;博士
Month
.from(
LocalDate.now()
)
.getDisplayName(
TextStyle.FULL_STANDALONE ,
new Locale( "es" , "ES" )
)
看到这个code run live at IdeOne.com。
julio
Month
class
by Wimmer is correct. But if all you want is the name of the month, use the Month
class。这可能更直接、更灵活。
getDisplayName
方法生成各种长度(缩写)的本地化字符串。
LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) );
Month month = Month.from( today );
String monthName = month.getDisplayName( TextStyle.FULL_STANDALONE , locale );
“独立”月份名称
请注意,TextStyle
提供月份名称的替代“独立”版本。在某些语言中,作为日期时间的一部分使用的名称与在没有特定日期时间的情况下通常使用的名称可能不同。
我用过这个并和我一起工作
LocalDate.now().month.getDisplayName(TextStyle.FULL,local)
我正在尝试格式化 LoacalDate,但没有找到任何信息。我需要格式化为另一种语言。案例只是我想用西班牙语获得一年中的月份。我正在尝试使用:
Locale locale = new Locale("es", "ES");
但我没有找到 SimpleDateFormat 或类似于 LocalDate 格式的格式。
LocalDate.now().getMonth();
谁能帮帮我?
抱歉,我使用了 Java 的较旧(并且仍然更常用)的日期时间 类,正如您所说的 SimpleDateFormat 是较旧 API 的一部分。
当您使用 java.time.LocalDate
时,您必须使用的格式化程序是 java.time.format.DateTimeFormatter
:
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM", aLocale);
final String month = LocalDate.now().format(formatter);
tl;博士
Month
.from(
LocalDate.now()
)
.getDisplayName(
TextStyle.FULL_STANDALONE ,
new Locale( "es" , "ES" )
)
看到这个code run live at IdeOne.com。
julio
Month
class
Month
class。这可能更直接、更灵活。
getDisplayName
方法生成各种长度(缩写)的本地化字符串。
LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) );
Month month = Month.from( today );
String monthName = month.getDisplayName( TextStyle.FULL_STANDALONE , locale );
“独立”月份名称
请注意,TextStyle
提供月份名称的替代“独立”版本。在某些语言中,作为日期时间的一部分使用的名称与在没有特定日期时间的情况下通常使用的名称可能不同。
我用过这个并和我一起工作
LocalDate.now().month.getDisplayName(TextStyle.FULL,local)