-2

可能重复:
Java 时区:为什么需要偏移

我的系统时区是。Asia/Calcutta我的要求是将一个时区的时间转换为另一个时区。

long l = 1223123123232l;// long value representing the date.
TimeZone tz = TimeZone.getTimeZone("Australia/Sydney");// First Time zone
long tzOff = tz.getOffset(l);
java.util.Date d = new Date(l-tzOff);   // WHY THIS??
DateFormat df = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
df.setTimeZone(TimeZone.getTimeZone("Africa/Asmara"));// Required Time zone
String s = df.format(d);
System.out.println(s);

我的代码有什么问题?

4

1 回答 1

0

这是一个重复的问题,已经解决。

使用Java 时区:为什么需要 Offset

或者

试试下面

Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");     
System.out.println("Default time, timezone EST : "+dateFormat.format(date));        
TimeZone t1 = TimeZone.getTimeZone("Asia/Calcutta");

dateFormat.setTimeZone(t1);
System.out.println("Converted time, timezone IST : "+dateFormat.format(date));
于 2013-02-05T16:24:11.070 回答