由于我是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.
推荐答案鉴于您知道获取日期时间对象的确切格式,可以使用 SimpleDateFormat类,您可以使用给定的字符串格式表示的日期格式,将字符串解析为日期.对于您的问题:
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中两种日期时间格式之间的区别?
发布评论