2012-05-03 29 views
0

我必须将实际日期和时间转换为millis,并将其转换为其他时区GMT + 3(我的时区为GMT-2)。我使用这段代码,但它返回给我时间,但进入我的时区....为什么?Android:将实际日期和时间转换为millis和其他时区

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT-3")); 
cal.get(Calendar.DAY_OF_MONTH); 
cal.get(Calendar.MONTH); 
cal.get(Calendar.YEAR); 
cal.get(Calendar.HOUR_OF_DAY); 
cal.get(Calendar.MINUTE); 
cal.get(Calendar.SECOND); 
cal.get(Calendar.MILLISECOND); 
long timez = cal.getTime().getTime(); 

回答

0

您需要使用SimpleDateFormat。日历始终使用您计算机上的默认配置时区。关于如何使用SimpleDateFormat实现此功能,请参见example

-1

getTime()方法返回同一时间。它与时区没有任何关系。

0
Date date = new Date(); 
    DateFormat firstFormat = new SimpleDateFormat(); 
    DateFormat secondFormat = new SimpleDateFormat(); 
    TimeZone firstTime = TimeZone.getTimeZone(args[0]); 
    TimeZone secondTime = TimeZone.getTimeZone(args[1]); 
    firstFormat.setTimeZone(firstTime); 
    secondFormat.setTimeZone(secondTime); 
    System.out.println("-->"+args[0]+": " + firstFormat.format(date)); 
    System.out.println("-->"+args[1]+": " + secondFormat.format(date)); 
    } 

其中arg [0]和arg 1是两个时区。 推荐这个LINK

相关问题