在Android中比较两次 [英] Comparing two times in android

查看:69
本文介绍了在Android中比较两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用下面的代码进行两次比较:

I have tried with the below code to compare two times:

   SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
    Date inTime = sdf.parse("11:00");
    Date outTime = sdf.parse("10:00");

    if(inTime.compareTo(outTime) > 0){
    Toast.makeText(this, "Out time should be greater than In time", Toast.LENGTH_SHORT).show();
    }

上面的代码工作正常.

但是,如果我将输入时间设为11:00,将输出时间设为12:00,那么我将获得超越敬酒的讯息.

But if I give in time as 11:00 and out time as 12:00, I am getting above toast message.

如果我从12:00到12:59抽出时间,我就会得到敬酒的讯息.在其他情况下,上述代码也可以正常工作.

I am getting above toast message if I give out time from 12:00 to 12:59. In other cases above code is working fine.

我在哪里做错了.请帮助我.

Where I am doing wrong. Please help me.

推荐答案

按如下所示更改SimpleDateFormat ...

Change SimpleDateFormat like below...

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

以下是模式:

H   Hour in day (0-23)  Number  0
k   Hour in day (1-24)  Number  24
K   Hour in am/pm (0-11)    Number  0
h   Hour in am/pm (1-12)    Number  12

这将起作用....

这篇关于在Android中比较两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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