android中两种日期时间格式之间的区别? [英] Difference between two datetime formats in android?

查看:103
本文介绍了android中两种日期时间格式之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我是android开发的新手,所以无法找到用于计算两种日期时间格式之间差异的代码.我的问题是.

As i am new to android development I am unable to find code for calculating the difference between two datetime formats. My question is.

我正在我的项目中使用Web服务,在该项目中,我得到如下的日期时间响应.

I am using webservice in my project where i am getting datetime response as follows..

 starttime  :- [2012-11-04 10:00:00]
 endtime    :- [2012-11-04 12:00:00]

我想在屏幕上显示为

   Today Timings  :-  2012-11-04   2 hours

方法需要计算两个日期之间的时差,并且需要在屏幕上显示时差.

Means need to calculate the difference between two dates and need to display the time difference on screen.

有人可以帮我这个忙吗?

Can anyone please help me with this.

推荐答案

鉴于您知道获取日期时间对象的确切格式,可以使用

Given you know the exact format in which you are getting the date-time object, you could use the SimpleDateFormat class in Android which allows you to parse a string as a date given the format of the date expressed in the string. For your question:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startTime = sdf.parse(<startTime/endTime String>, 0);

类似地解析您的结束时间,然后可以使用各个对象的getTime()获得差异.

Similarly parse your endtime, and then the difference can be obtained using getTime() of the individual objects.

long diff = endTime.getTime() - startTime.getTime()

然后就像使用以下方法将时差转换为小时数一样简单:

Then it's as simple as converting the difference to hours using:

int hours = (int)(diff/(60*60*1000));

希望有帮助.

这篇关于android中两种日期时间格式之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆