我正在尝试将本地时区的日期转换为 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 中的 GMT 2 o/p,我这样做了:
日历日历 = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
日历.setTime(gmtDate);
System.out.println("GMT 3 = "+calendar.getTime());
但格林尼治标准时间下午 3 点仍处于当地 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));
}
}
Output
Current Time: Wed Mar 10 11:21:18 IST 2010
GMT Time: 3/10/10 5:51 AM
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)