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 format
method of the SimpleDateFormat
class returns a String
composed of digits and symbols. For example, in the String
"Friday, April 10, 2009," the symbols are "Friday" and "April." If the symbols encapsulated in SimpleDateFormat
don't meet your needs, you can change them with the DateFormatSymbols
. You can change symbols that represent names for months, days of the week, and time zones, among others. The following table lists the DateFormatSymbols
methods that allow you to modify the symbols:
Setter Method | Example of a Symbol the Method Modifies |
---|---|
setAmPmStrings |
PM |
setEras |
AD |
setMonths |
December |
setShortMonths |
Dec |
setShortWeekdays |
Tue |
setWeekdays |
Tuesday |
setZoneStrings |
PST |
The following example invokes setShortWeekdays
to change the short names of the days of the week from lowercase to uppercase characters. The full source code for this example is in DateFormatSymbolsDemo
. The first element in the array argument of setShortWeekdays
is a null String
. Therefore the array is one-based rather than zero-based. The SimpleDateFormat
constructor accepts the modified DateFormatSymbols
object as an argument. Here is the source code:
Date today; String result; SimpleDateFormat formatter; DateFormatSymbols symbols; String[] defaultDays; String[] modifiedDays; symbols = new DateFormatSymbols( new Locale("en", "US")); defaultDays = symbols.getShortWeekdays(); for (int i = 0; i < defaultDays.length; i++) { System.out.print(defaultDays[i] + " "); } System.out.println(); String[] capitalDays = { "", "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" }; symbols.setShortWeekdays(capitalDays); modifiedDays = symbols.getShortWeekdays(); for (int i = 0; i < modifiedDays.length; i++) { System.out.print(modifiedDays[i] + " "); } System.out.println(); System.out.println(); formatter = new SimpleDateFormat("E", symbols); today = new Date(); result = formatter.format(today); System.out.println("Today's day of the week: " + result);
The preceding code generates this output:
Sun Mon Tue Wed Thu Fri Sat SUN MON TUE WED THU FRI SAT Today's day of the week: MON