下面的代码演示了如何格式化特定语言环境的日期信息。在示例中,使用java.text.SimpleDateFormat该类。
package org.nhooo.example.text;
import java.util.Locale;
import java.util.Date;
import java.text.SimpleDateFormat;
public class FormatDateLocale {
public static void main(String[] args) {
// 定义我们将用于的语言环境数组
// 格式化日期信息。
Locale[] locales = new Locale[] {
Locale.JAPAN,
Locale.CHINA,
Locale.KOREA,
Locale.TAIWAN,
Locale.ITALY,
Locale.FRANCE,
Locale.GERMAN
};
// 获取当前日期时间的实例
Date today = new Date();
// 迭代上面定义的整个Locale并创建一个long
// 使用SimpleDateFormat.getDateInstance()格式化日期
// 格式,语言环境和日期信息。
for (Locale locale : locales) {
System.out.printf("Date format in %s = %s%n",
locale.getDisplayName(), SimpleDateFormat.getDateInstance(
SimpleDateFormat.LONG, locale).format(today));
}
}
}我们的代码的结果是:
Date format in Japanese (Japan) = 2018年2月15日 Date format in Chinese (China) = 2018年2月15日 Date format in Korean (South Korea) = 2018년 2월 15일 Date format in Chinese (Taiwan) = 2018年2月15日 Date format in Italian (Italy) = 15 febbraio 2018 Date format in French (France) = 15 février 2018 Date format in German = 15. Februar 2018