Java I18n 简明教程

Java Internationalization - DateFormat Class

java.text.DateFormat 类根据语言环境格式化日期。由于不同的国家使用不同的格式显示日期。该类在处理应用程序的国际化中的日期时非常有用。以下示例展示了如何创建和使用 DateFormat 类。

java.text.DateFormat class formats dates as per the locale. As different coutries use different formats to display dates. This class is extremely useful in dealing with dates in Internationalization of application. Following example show how to create and use DateFormat Class.

Example

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class I18NTester {
   public static void main(String[] args) {
      Locale locale = new Locale("da","DK");

      DateFormat dateFormat = DateFormat.getDateInstance();

      System.out.println(dateFormat.format(new Date()));

      dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, locale);

      System.out.println(dateFormat.format(new Date()));
   }
}

Output

它将打印以下结果。

It will print the following result.

Nov 29, 2017
29-11-2017