在java中将本地时区转换为GMT

我尝试将当地时区的转换日期改为GMT,我做了一些这样的事情

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); String gmtStrDate = sdf.format(Calendar.getInstance().getTime()); System.out.println("GMT 1"+gmtStrDate); Date gmtDate = sdf.parse(gmtStrDate); System.out.println("GMT 2"+gmtDate); 

我在GMT tz获得GMT 1 o / p但是GMT 2 o / p再次变为当地时区…任何电话我可以为什么?

要获得GMT 2 o / p GMT,我确实喜欢这样:

日历日历= Calendar.getInstance(TimeZone.getTimeZone(“GMT”)); calendar.setTime(GMTDATE);

  System.out.println("GMT 3 = "+calendar.getTime()); 

但是GMT 3o / p仍然在当地tz。 请对此提出建议。

 import java.util.*; import java.text.*; public class CurrentTimeToGMT{ public static void main(String args[]){ Date date = new Date(); DateFormat gmtFormat = new SimpleDateFormat(); TimeZone gmtTime = TimeZone.getTimeZone("GMT"); gmtFormat.setTimeZone(gmtTime); System.out.println("Current Time: "+date); System.out.println("GMT Time: " + gmtFormat.format(date)); } } 

产量

当前时间:2010年3月10日星期三11:21:18 IST 2010

格林威治标准时间:3/10/10 5:51 AM

在“解析”之后,您必须在打印前再次格式化,例如:

 System.out.println("GMT 2"+sdf.format(gmtDate)); 

编辑:原因是您的gmtStrDate不存储任何时区信息