如何在android中进行两次比较 [英] how to compare two times in android

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

问题描述

两个日期正在比较,但时间没有正确比较。
这是我的代码

Two Dates are Comparing but Time is not comparing properly. This is my Code

final String setdate = cursor.getString(cursor.getColumnIndex(cursor.getColumnName(4)));
        final String settime = cursor.getString(cursor.getColumnIndex(cursor.getColumnName(5)));
 Calendar calendar1 = Calendar.getInstance();
        SimpleDateFormat formatter1 = new SimpleDateFormat("dd/M/yyyy");
        String currentDate = formatter1.format(calendar1.getTime());

        Calendar calendar2 = Calendar.getInstance();
        SimpleDateFormat formatter2 = new SimpleDateFormat("h:mm");
        String currentTime = formatter2.format(calendar2.getTime());


        if(currentDate.compareTo(setdate)>=0)
           {
              if(currentTime.compareTo(settime)>=0) {


                    myCheckBox.setChecked(true);
                    myCheckBox.setEnabled(false);


               }

           }

我怎么能比较两次。在数据库的日期和时间字段是不同的。所以请帮助我。

How can I compare two times.In database date and time field are different.So help me please.

推荐答案

Calendar calendar1 = Calendar.getInstance();
SimpleDateFormat formatter1 = new SimpleDateFormat("dd/M/yyyy h:mm");
String currentDate = formatter1.format(calendar1.getTime());

final String dateString = cursor.getString(4);
final String timeString = cursor.getString(5);
String datadb =dateString+" "+timeString;

//  Toast.makeText(context,"databse date:-"+datadb+"Current Date :-"+currentDate,Toast.LENGTH_LONG).show();

if(currentDate.compareTo(datadb)>=0) {
    myCheckBox.setChecked(true);
    myCheckBox.setEnabled(false);
}

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

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