通过Android获得GMT时间

通过Android获得GMT时间,第1张

概述我一直在StackOverflow中深入研究这个问题AndroidgetCurrentUTCtime和HowcanIgetthecurrentdateandtimeinUTCorGMTinJava?我尝试了两种方法来获取GMT手机的当前时间.我在西班牙,差异是GMT2.所以让我们看一个例子:1º尝试:我创建了一个格式并将其应用于System.cu

我一直在StackOverflow中深入研究这个问题
Android get Current UTC time

How can I get the current date and time in UTC or GMT in Java?

我尝试了两种方法来获取GMT手机的当前时间.我在西班牙,差异是GMT 2.所以让我们看一个例子:
1º尝试:我创建了一个格式并将其应用于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;

它确实有效.

有任何想法吗?

非常感谢,

大卫.

解决方法:

据我读到calendar.getTimeInMillis();以毫秒为单位返回UTC时间.我使用以下代码并将其与此站点http://www.xav.com/time.cgi中的Epoch进行了比较.

public int GetUnixTime(){    Calendar calendar = Calendar.getInstance();    long Now = calendar.getTimeInMillis();    int utc = (int)(Now / 1000);    return (utc);}

Giora

总结

以上是内存溢出为你收集整理的通过Android获得GMT时间全部内容,希望文章能够帮你解决通过Android获得GMT时间所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1102496.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存