Java I18n 简明教程

Java Internationalization - Formatting Time

DateFormat 类提供了各种格式来格式化时间。应使用 DateFormat.getTimeInstance() 方法。请参见以下示例。

DateFormat class provides various formats to format the time. DateFormat.getTimeInstance() method is to be used. See the example below.

Example

在以下示例中,我们将展示如何使用不同的格式来格式化时间。

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

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

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

      DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.DEFAULT);

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

      dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT);

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

      dateFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM);

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

      dateFormat = DateFormat.getTimeInstance(DateFormat.LONG);

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

      dateFormat = DateFormat.getTimeInstance(DateFormat.FULL);

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

   }
}

Output

它将打印以下结果。

It will print the following result.

2:36:09 PM
2:36 PM
2:36:09 PM
2:36:09 PM IST
2:36:09 PM India Standard Time