android设置gmt时间格式,通过Android获得GMT时间

我尝试了两种方法来获取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

标签:gmt,android,date-format

来源: https://codeday.me/bug/20190926/1819129.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值