The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
The Date-Time API provides enums for specifying days of the week and months of the year.Date-Time API提供枚举,用于指定一周中的几天和一年中的几个月。
The DayOfWeek enum consists of seven constants that describe the days of the week: MONDAY through SUNDAY. DayOfWeek枚举由七个常量组成,它们描述一周中的几天:周一到周日。The integer values of the DayOfWeek constants range from 1 (Monday) through 7 (Sunday). DayOfWeek常量的整数值范围为1(星期一)到7(星期日)。Using the defined constants (DayOfWeek.FRIDAY) makes your code more readable.使用定义的常量(DayOfWeek.FRIDAY)可以使代码更具可读性。
This enum also provides a number of methods, similar to the methods provided by the temporal-based classes. 该枚举还提供了许多方法,类似于基于时态的类提供的方法。For example, the following code adds 3 days to "Monday" and prints the result. 例如,下面的代码将“星期一”添加3天并打印结果。The output is "THURSDAY":输出为“星期四”:
System.out.printf("%s%n", DayOfWeek.MONDAY.plus(3));
By using the getDisplayName(TextStyle, Locale) method, you can retrieve a string to identify the day of the week in the user's locale. 通过使用getDisplayName(TextStyle, Locale)方法,您可以检索一个字符串来标识用户区域设置中的星期几。The TextStyle enum enables you to specify what sort of string you want to display: FULL, NARROW (typically a single letter), or SHORT (an abbreviation). TextStyle枚举允许您指定要显示的字符串类型:FULL、NARROW(通常为单个字母)或短(缩写)。The STANDALONE TextStyle constants are used in some languages where the output is different when used as part of a date than when it is used by itself. 独立TextStyle常量在某些语言中使用,其中作为日期的一部分使用时的输出与单独使用时的输出不同。The following example prints the three primary forms of the TextStyle for "Monday":以下示例打印“星期一”的TextStyle的三种主要形式:
DayOfWeek dow = DayOfWeek.MONDAY; Locale locale = Locale.getDefault(); System.out.println(dow.getDisplayName(TextStyle.FULL, locale)); System.out.println(dow.getDisplayName(TextStyle.NARROW, locale)); System.out.println(dow.getDisplayName(TextStyle.SHORT, locale));
This code has the following output for the en locale:对于en语言环境,此代码具有以下输出:
Monday M Mon
The Month enum includes constants for the twelve months, JANUARY through DECEMBER. Month枚举包括12个月(JANUARY至DECEMBER)的常量。As with the DayOfWeek enum, the Month enum is strongly typed, and the integer value of each constant corresponds to the ISO range from 1 (January) through 12 (December). 与DayOfWeek枚举一样,Month枚举是强类型的,每个常量的整数值对应于从1(一月)到12(十二月)的ISO范围。Using the defined constants (Month.SEPTEMBER) makes your code more readable.使用已定义的常量(Month.SEPTEMBER)可以使代码更具可读性。
The Month enum also includes a number of methods. Month枚举还包括许多方法。The following line of code uses the maxLength method to print the maximum possible number of days in the month of February. 以下代码行使用maxLength方法打印2月份的最大可能天数。The output is "29":输出为“29”:
System.out.printf("%d%n", Month.FEBRUARY.maxLength());
The Month enum also implements the getDisplayName(TextStyle, Locale) method to retrieve a string to identify the month in the user's locale using the specified TextStyle. Month枚举还实现了getDisplayName(TextStyle, Locale)方法来检索字符串,以使用指定的TextStyle在用户的区域设置中标识月份。If a particular TextStyle is not defined, then a string representing the numeric value of the constant is returned. 如果未定义特定的TextStyle,则返回表示常量数值的字符串。The following code prints the month of August using the three primary text styles:以下代码使用三种主要文本样式打印8月份:
Month month = Month.AUGUST; Locale locale = Locale.getDefault(); System.out.println(month.getDisplayName(TextStyle.FULL, locale)); System.out.println(month.getDisplayName(TextStyle.NARROW, locale)); System.out.println(month.getDisplayName(TextStyle.SHORT, locale));
This code has the following output for the en locale:对于en语言环境,此代码具有以下输出:
August A Aug