如何在格式化时从 Java 8 中的日期获取月份的全名
How to get full name of month from date in Java 8 while formatting
使用新的 Java 8 java.time API,我需要转换 LocalDate 并获取月份和日期的全名。比如 March(不是 Mar)和 Monday(不是 Mon)。 3 月 13 日星期五的格式应类似于 3 月 13 日星期五。而不是 3 月 13 日星期五。
您要查找的字符串是MMMM
。
tl;博士
使用自动本地化。无需指定格式模式。
localDate.format(
DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL )
.withLocale( Locale.UK )
)
Monday, 23 January 2017
LocalDate
.of( 2017 , Month.JANUARY , 23 )
.getMonth()
.getDisplayName(
TextStyle.FULL ,
Locale.CANADA_FRENCH
)
janvier
月份
从字面上理解你的标题,我会使用方便的 Month
枚举。
LocalDate ld = LocalDate.of( 2017 , Month.JANUARY , 23 );
Month month = ld.getMonth() ; // Returns a `Month` object, whereas `getMonthValue` returns an integer month number (1-12).
让java.time做自动本地化的工作。要本地化,请指定:
例如:
String output = month.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ; // Or Locale.US, Locale.KOREA, etc.
janvier
日期
如果您希望将整个日期本地化,请让 DateTimeFormatter
完成工作。这里我们使用 FormatStyle
而不是 TextStyle
.
示例:
Locale l = Locale.CANADA_FRENCH ; // Or Locale.US, Locale.KOREA, etc.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL )
.withLocale( l ) ;
String output = ld.format( f );
dimanche 23 janvier 2107
关于java.time
java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
Joda-Time project, now in maintenance mode, advises migration to the java.time 类.
要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310。
您可以直接与您的数据库交换java.time objects。使用 JDBC driver compliant with JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.*
类.
java.time类在哪里获取?
- Java SE 8, Java SE 9, Java SE 10,及以后
- Built-in。
- 标准 Java API 的一部分,带有捆绑实施。
- Java 9 添加了一些小功能和修复。
- Java SE 6 and Java SE 7
- java.time 的大部分功能是 back-ported 到 Java ThreeTen-Backport 中的 6 和 7。
- Android
- Android java.time 类.
捆绑实施的更高版本
- 对于较早的 Android (<26),ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP…。
ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
是的。现在可以做到了。
LocalDate dd = new LocalDate(); //pass in a date value or params(yyyy,mm)
String ss = dd.monthOfYear.getAsText(); // will give the full name of the month
String sh = dd.monthOfYear.getAsShortText(); // shortform
import java.time.LocalDate;
只需使用 getDayOfWeek()
LocalDate.of(year, month, day).getDayOfWeek().name()
您可以将其用作
public static String dayName(int month, int day, int year) {
return LocalDate.of(year, month, day).getDayOfWeek().name();
}
使用新的 Java 8 java.time API,我需要转换 LocalDate 并获取月份和日期的全名。比如 March(不是 Mar)和 Monday(不是 Mon)。 3 月 13 日星期五的格式应类似于 3 月 13 日星期五。而不是 3 月 13 日星期五。
您要查找的字符串是MMMM
。
tl;博士
使用自动本地化。无需指定格式模式。
localDate.format(
DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL )
.withLocale( Locale.UK )
)
Monday, 23 January 2017
LocalDate
.of( 2017 , Month.JANUARY , 23 )
.getMonth()
.getDisplayName(
TextStyle.FULL ,
Locale.CANADA_FRENCH
)
janvier
月份
从字面上理解你的标题,我会使用方便的 Month
枚举。
LocalDate ld = LocalDate.of( 2017 , Month.JANUARY , 23 );
Month month = ld.getMonth() ; // Returns a `Month` object, whereas `getMonthValue` returns an integer month number (1-12).
让java.time做自动本地化的工作。要本地化,请指定:
例如:
String output = month.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ; // Or Locale.US, Locale.KOREA, etc.
janvier
日期
如果您希望将整个日期本地化,请让 DateTimeFormatter
完成工作。这里我们使用 FormatStyle
而不是 TextStyle
.
示例:
Locale l = Locale.CANADA_FRENCH ; // Or Locale.US, Locale.KOREA, etc.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL )
.withLocale( l ) ;
String output = ld.format( f );
dimanche 23 janvier 2107
关于java.time
java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
Joda-Time project, now in maintenance mode, advises migration to the java.time 类.
要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310。
您可以直接与您的数据库交换java.time objects。使用 JDBC driver compliant with JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.*
类.
java.time类在哪里获取?
- Java SE 8, Java SE 9, Java SE 10,及以后
- Built-in。
- 标准 Java API 的一部分,带有捆绑实施。
- Java 9 添加了一些小功能和修复。
- Java SE 6 and Java SE 7
- java.time 的大部分功能是 back-ported 到 Java ThreeTen-Backport 中的 6 和 7。
- Android
- Android java.time 类. 捆绑实施的更高版本
- 对于较早的 Android (<26),ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP…。
ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
是的。现在可以做到了。
LocalDate dd = new LocalDate(); //pass in a date value or params(yyyy,mm)
String ss = dd.monthOfYear.getAsText(); // will give the full name of the month
String sh = dd.monthOfYear.getAsShortText(); // shortform
import java.time.LocalDate;
只需使用 getDayOfWeek()
LocalDate.of(year, month, day).getDayOfWeek().name()
您可以将其用作
public static String dayName(int month, int day, int year) {
return LocalDate.of(year, month, day).getDayOfWeek().name();
}