Java I18n 简明教程

Java Internationalization - Formatting Date and Time

DateFormat 类通过提供各种格式,将日期和时间组合起来进行格式化。须使用 DateFormat.getDateTimeInstance() 方法。请参见下面的示例。

DateFormat class provides various formats to format the date and time together. DateFormat.getDateTimeInstance() method is to be used. See the example below.

Example

在以下示例中,我们将演示如何使用不同的格式对日期和时间进行格式化。

In following example we’ll show how to use different formats to format date and time.

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

public class I18NTester {
   public static void main(String[] args) {

      DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT);

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

      dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);

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

      dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);

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

      dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);

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

      dateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);

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

   }
}

Output

它将打印以下结果。

It will print the following result.

Jun 7, 2024, 2:34:35 PM
6/7/24, 2:34 PM
Jun 7, 2024, 2:34:35 PM
June 7, 2024 at 2:34:35 PM IST
Friday, June 7, 2024 at 2:34:35 PM India Standard Time