Java I18n 简明教程

Java Internationalization - UTC

UTC 代表协调世界时。它是一种时间标准,通常在全世界使用。所有时区都与 UTC 比较计算为偏移量。例如,丹麦哥本哈根的时间为 UTC + 1,表示 UTC 时间加上 1 小时。它与夏令时无关,应该用于在数据库中存储日期和时间。

UTC stands for Co-ordinated Universal Time. It is time standard and is commonly used across the world. All timezones are computed comparatively with UTC as offset. For example, time in Copenhagen, Denmark is UTC + 1 means UTC time plus one hour. It is independent of Day light savings and should be used to store date and time in databases.

Conversion of Time Zones

以下示例将展示各种时区的转换。我们将打印一天中的小时和毫秒数。第一个会发生变化,第二个将保持不变。

Following example will showcase conversion of various timezones. We’ll print hour of the day and time in milliseconds. First will vary and second will remain same.

Example

import java.text.ParseException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

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

      Calendar date = new GregorianCalendar();

      date.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
      date.set(Calendar.HOUR_OF_DAY, 12);

      System.out.println("UTC: " + date.get(Calendar.HOUR_OF_DAY));
      System.out.println("UTC: " + date.getTimeInMillis());

      date.setTimeZone(TimeZone.getTimeZone("Europe/Copenhagen"));
      System.out.println("CPH: " + date.get(Calendar.HOUR_OF_DAY));
      System.out.println("CPH: " + date.getTimeInMillis());

      date.setTimeZone(TimeZone.getTimeZone("America/New_York"));
      System.out.println("NYC: " + date.get(Calendar.HOUR_OF_DAY));
      System.out.println("NYC: " + date.getTimeInMillis());
   }
}

它将打印以下结果。

It will print the following result.

UTC: 12
UTC: 1511956997540
CPH: 13
CPH: 1511956997540
NYC: 7
NYC: 1511956997540

Available Time Zones

以下示例将展示系统可用的时区。

Following example will showcase the timezones available with the system.

Example

import java.text.ParseException;
import java.util.TimeZone;

public class I18NTester {
   public static void main(String[] args) throws ParseException {
      String[] availableIDs = TimeZone.getAvailableIDs();

      for(String id : availableIDs) {
         System.out.println("Timezone = " + id);
      }
   }
}

它将打印以下结果。

It will print the following result.

Timezone = Africa/Abidjan
Timezone = Africa/Accra
...
Timezone = VST