📄 e372. converting times between time zones.txt
字号:
There is a convenient setTimeZone() method in the Calendar object. However, it doesn't always return the correct results when used after a calendar field is set. This example demonstrates a more reliable way to convert a specific time from one time zone to another. It involves creating two Calendar instances and transfering the UTC (Coordinate Universal Time) from one to the other. The UTC is a representation of time and date that is independent of time zones.
// Given a local time of 10am, get the time in Japan
// Create a Calendar object with the local time zone
Calendar local = new GregorianCalendar();
local.set(Calendar.HOUR_OF_DAY, 10); // 0..23
local.set(Calendar.MINUTE, 0);
local.set(Calendar.SECOND, 0);
// Create an instance using Japan's time zone and set it with the local UTC
Calendar japanCal = new GregorianCalendar(TimeZone.getTimeZone("Japan"));
japanCal.setTimeInMillis(local.getTimeInMillis());
// Get the foreign time
int hour = japanCal.get(Calendar.HOUR); // 3
int minutes = japanCal.get(Calendar.MINUTE); // 0
int seconds = japanCal.get(Calendar.SECOND); // 0
boolean am = japanCal.get(Calendar.AM_PM) == Calendar.AM; //true
// Given a time of 10am in Japan, get the local time
japanCal = new GregorianCalendar(TimeZone.getTimeZone("Japan"));
japanCal.set(Calendar.HOUR_OF_DAY, 10); // 0..23
japanCal.set(Calendar.MINUTE, 0);
japanCal.set(Calendar.SECOND, 0);
// Create a Calendar object with the local time zone and set
// the UTC from japanCal
local = new GregorianCalendar();
local.setTimeInMillis(japanCal.getTimeInMillis());
// Get the time in the local time zone
hour = local.get(Calendar.HOUR); // 5
minutes = local.get(Calendar.MINUTE); // 0
seconds = local.get(Calendar.SECOND); // 0
am = local.get(Calendar.AM_PM) == Calendar.AM; // false
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -