2013-02-05 23 views
-2

可能重复:
Java Timezone: why Offset is neededJava的时区转换问题

我的系统时区为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); 

我的代码有什么问题?

+3

你不是最近发布的同一个问题

试试? – NPE

+0

是的,先生,但我现在面临这个问题,需要紧急解决这个问题? –

回答

0

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

使用Java Timezone: why Offset is needed

或低于

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));