# 更改日期格式符号

> 原文： [https://docs.oracle.com/javase/tutorial/i18n/format/dateFormatSymbols.html](https://docs.oracle.com/javase/tutorial/i18n/format/dateFormatSymbols.html)

* * *

**Version note:** This Date and Time section uses the date and time APIs in the `java.util` package. The `java.time` APIs, available in the JDK 8 release, provides a comprehensive date and time model that offers significant improvements over the `java.util` classes. The `java.time` APIs are described in the [Date Time](../../datetime/index.html) trail. The [Legacy Date-Time Code](../../datetime/iso/legacy.html) page might be of particular interest.

* * *

`SimpleDateFormat`类的`format`方法返回由数字和符号组成的`String`。例如，在`String`“2009 年 4 月 10 日星期五”中，符号为“星期五”和“四月”。如果封装在`SimpleDateFormat`中的符号不​​符合您的需要，您可以使用 [`DateFormatSymbols`](https://docs.oracle.com/javase/8/docs/api/java/text/DateFormatSymbols.html) 更改它们。您可以更改代表月份，星期几和时区等名称的符号。下表列出了允许您修改符号的`DateFormatSymbols`方法：

<caption>`DateFormatSymbol` Methods</caption>
| 二传法 | 方法修改的符号示例 |
| --- | --- |
| `setAmPmStrings` | 下午 |
| `setEras` | 广告 |
| `setMonths` | 十二月 |
| `setShortMonths` | 十二月 |
| `setShortWeekdays` | 星期二 |
| `setWeekdays` | 星期二 |
| `setZoneStrings` | 太平洋标准时间 |

以下示例调用`setShortWeekdays`将星期几的短名称从小写字符更改为大写字符。此示例的完整源代码位于 [`DateFormatSymbolsDemo`](examples/DateFormatSymbolsDemo.java) 中。 `setShortWeekdays`的数组参数中的第一个元素是 null `String`。因此，阵列是基于一个而不是基于零的。 `SimpleDateFormat`构造函数接受修改后的`DateFormatSymbols`对象作为参数。这是源代码：

```
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);

```

上面的代码生成了这个输出：

```
 Sun Mon Tue Wed Thu Fri Sat 
 SUN MON TUE WED THU FRI SAT 

Today's day of the week: MON

```