【发布时间】:2011-08-26 06:33:33
【问题描述】:
我在 StackOverflow 中一直在研究这个问题一段时间 Android get Current UTC time 和 How can I get the current date and time in UTC or GMT in Java?
我尝试了两种方法来获取我手机的当前时间(格林威治标准时间)。我在西班牙,时差是 GMT+2。所以让我们看一个例子: 1º attemp:我创建了一个格式并将其应用于 System.currentTimeMillis();
DateFormat dfgmt = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dfgmt.setTimeZone(TimeZone.getTimeZone("GMT"));
String gmtTime = dfgmt.format(new Date());
//Using System.currentTimeMillis() is the same as new Date()
Date dPhoneTime = dfgmt.parse(gmtTime);
Long phoneTimeUTC = dPhoneTime.getTime();
我需要将那个时间减去另一个时间,这就是我对 Long 进行演员表的原因。
DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date arrivalDate = df.parse(item.getArrivalDate());
//the String comes from JSON and is for example:"UTC_arrival":"2011-05-16 18:00:00"
//which already is in UTC format. So the DateFormat doesnt have the GMT paramater as dfgmt
diff = arrival.getTime() - phoneTimeUTC ;
我也试过这个:
Calendar aGMTCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Long phoneTimeUTC = aGMTCalendar.getTimeInMillis()
我仍然没有得到正确的区别。但如果我这样做:
Long phoneTimeUTC = aGMTCalendar.getTimeInMillis()-3600000*2;
确实可以。
有什么想法吗?
非常感谢,
大卫。
【问题讨论】:
标签: android date-format gmt