【发布时间】:2014-02-13 19:42:46
【问题描述】:
我面临一个问题:我想获得 GMT TimeZone 的当前时间。 我正在使用以下代码,如下所示:
TimeZone timeZoneGmt = TimeZone.getTimeZone("GMT");
long gmtCurrentTime = getCurrentTimeInSpecificTimeZone(timeZoneGmt);
public static long getCurrentTimeInSpecificTimeZone(TimeZone timeZone) {
Calendar cal = Calendar.getInstance();
cal.setTimeZone(timeZone);
long finalValue = 0;
SimpleDateFormat sdf = new SimpleDateFormat(
"MMM dd yyyy hh:mm:ss:SSSaaa");
sdf.setTimeZone(timeZone);
Date finalDate = null;
String date = sdf.format(cal.getTime());
try {
finalDate = sdf.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finalValue = finalDate.getTime();
return finalValue;
}
如上所示,上述方法
格式化时String date = sdf.format(cal.getTime());
我在格林威治标准时间得到正确的当前时间,但正如我通过以下代码解析的那样:
finalDate=sdf.parse(date);
日期已从当前 GMT 时间更改为 2013 年 IST 15:35:16,这是我系统的当前时间。
我也以另一种方式尝试使用日历:
TimeZone timeZoneGmt=TimeZone.get("GMT");
Calendar calGmt = Calendar.getInstance();
calGmt.setTimeZone(timeZoneGmt);
long finalGmtValue = 0;
finalGmtValue = calGmt.getTimeInMillis();
System.out.println("Date......" + calGmt.getTime());
但仍将日期作为我系统的当前时间 Thu Jan 23 15:58:16 IST 2014 未获得 GMT 当前时间。
【问题讨论】:
标签: java android timezone simpledateformat