Java如何在时区之间转换时间?
package org.nhooo.example.util;import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class TimeZoneExample {
public static void main(String[] args) {
// 创建一个日历对象并根据本地时间设置时间
// 时区
Calendar localTime = Calendar.getInstance();
localTime.set(Calendar.HOUR, 17);
localTime.set(Calendar.MINUTE, 15);
localTime.set(Calendar.SECOND, 20);
int hour = localTime.get(Calendar.HOUR);
int minute = localTime.get(Calendar.MINUTE);
int second = localTime.get(Calendar.SECOND);
// 打印当地时间
System.out.printf("Local time : %02d:%02d:%02dn", hour, minute, second);
//创建一个表示德国时区的日历对象。然后我们
// 用当地时间的值弄湿日历的时间
Calendar germanyTime = new GregorianCalendar(TimeZone.getTimeZone("Germany"));
germanyTime.setTimeInMillis(localTime.getTimeInMillis());
hour = germanyTime.get(Calendar.HOUR);
minute = germanyTime.get(Calendar.MINUTE);
second = germanyTime.get(Calendar.SECOND);
// 打印当地时间 in Germany time zone
System.out.printf("Germany time: %02d:%02d:%02dn", hour, minute, second);
}
}
以上是 Java如何在时区之间转换时间? 的全部内容, 来源链接: utcz.com/z/330743.html