android中两种日期时间格式之间的区别?

编程入门 行业动态 更新时间:2024-10-24 20:18:44
本文介绍了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.

推荐答案

鉴于您知道获取日期时间对象的确切格式,可以使用 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中两种日期时间格式之间的区别?

本文发布于:2023-10-18 23:54:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1505793.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两种   区别   日期   格式   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!