Android:将格式为“ yyyy-mm-dd hh:mm:ss”的时间与当前时间进行比较 [英] Android: Compare time in this format `yyyy-mm-dd hh:mm:ss` to the current moment

查看:497
本文介绍了Android:将格式为“ yyyy-mm-dd hh:mm:ss”的时间与当前时间进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以2013-10-17 15:45:01的格式获取设备上的当前时间?

I want to get the current time on the device in the format: 2013-10-17 15:45:01 ?

服务器向我发送日期为格式如上的字符串形式的对象。现在我想获取手机的当前时间,然后检查是否有超过5分钟的差异?

The server sends me the date of an object in the format above as a string. Now i want to get the phones current time and then check if there is a difference of say more than 5 minutes?

所以A:我如何获取设备的当前时间在此格式中: 2013-10-17 15:45:01

So A: How can i get the devices current time in this fomat: 2013-10-17 15:45:01

B我该如何计算出差异

B how can I work out the difference between the two.

推荐答案

您可以使用 SimpleDateFormat 指定模式您想要的:

You can use SimpleDateFormat to specify the pattern you want:

new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new java.util.Date())

但是,如果您只是想知道时差是否在一定范围内阈值,您应该只比较长值。如果您的阈值是5分钟,则这是 5 * 60 * 1000 毫秒,因此您可以使用相同的 SimpleDateFormat 调用它的 parse 方法并检查长值。

However, if you just want to know whether the time difference is within a certain threshold, you should probably just compare long values. If your threshold is 5 minutes, then this is 5 * 60 * 1000 milliseconds so you can use the same SimpleDateFormat by calling it's parse method and check the long values.

示例:

new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2013-10-13 14:54:03").getTime()

这篇关于Android:将格式为“ yyyy-mm-dd hh:mm:ss”的时间与当前时间进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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