System.out.println("abc")
1. java.text.NumberFormat
// currentLocal 为 Local对象
// 数字格式器
NumberFormat numberFormatter = NumberFormat.getNumberInstance(currentLocale);
// 货币格式器
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
// 百分数格式器
NumberFormat percentFormatter = NumberFormat.getPercentInstance(currentLocale);
// 有两个主要的API
// 格式化一个数并将所得文本添加到给定字符串缓冲区。
public StringBuffer format(Object number,StringBuffer toAppendTo,FieldPosition pos)
//解析字符串中的文本,以生成一个 Number。
public final Object parseObject(String source,ParsePosition pos)
2. java.text.DateFormat
a . 获取时间格式器
/ 主要的API // 获取日期格式器,该格式器具有给定语言环境的给定格式化风格。
// 主要的API
// 获取日期格式器,该格式器具有给定语言环境的给定格式化风格。
public static final DateFormat getDateInstance(int style,Locale aLocale)
style风格如下图
Sample Date Formats | ||
Style | U.S. Locale | French Locale |
DEFAULT | 10-Apr-98 | 10 avr 98 |
SHORT | 4/10/98 | 10/04/98 |
MEDIUM | 10-Apr-98 | 10 avr 98 |
LONG | April 10, 1998 | 10 avril 1998 |
FULL | Friday, April 10, 1998 | vendredi, 10 avril 1998 |
b. 获取时间格式器
// 获取时间格式器,该格式器具有给定语言环境的给定格式化风格。
// 获取时间格式器,该格式器具有给定语言环境的给定格式化风格。
public static final DateFormat getTimeInstance(int style,Locale aLocale)
style风格如下图
Sample Time Formats | ||
Style | U.S. Locale | German Locale |
DEFAULT | 3:58:45 PM | 15:58:45 |
SHORT | 3:58 PM | 15:58 |
MEDIUM | 3:58:45 PM | 15:58:45 |
LONG | 3:58:45 PM PDT | 15:58:45 GMT+02:00 |
FULL | 3:58:45 oclock PM PDT | 15.58 Uhr GMT+02:00 |
c. 获取日期时间格式器
//
public static final DateFormat getDateTimeInstance(int style,Locale aLocale)
Sample Date and Time Formats | ||
Style | U.S. Locale | French Locale |
DEFAULT | 25-Jun-98 1:32:19 PM | 25 jun 98 22:32:20 |
SHORT | 6/25/98 1:32 PM | 25/06/98 22:32 |
MEDIUM | 25-Jun-98 1:32:19 PM | 25 jun 98 22:32:20 |
LONG | June 25, 1998 1:32:19 PM PDT | 25 juin 1998 22:32:20 GMT+02:00 |
FULL | Thursday, June 25, 1998 1:32:19 o'clock PM PDT | jeudi, 25 juin 1998 22 h 32 GMT+02:00 |
3. java.text.SimpleFormat (见java 日期博客)
扩展
4. java.text.DateFormatSymbols
如果对所需的日期格式不满意,可以为特定语言环境创建具有特定格式模式的日期-时间格式器, 主要实现是一系列的set方法.
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(result);
The preceding code generates this output:
Sun Mon Tue Wed Thu Fri Sat
SUN MON TUE WED THU FRI SAT
WED