在android中比较时间

时间:2014-11-14 13:27:04

标签: android android-timepicker

我正在创建一个应用程序,我要求用户从时间选择器中选择一个时间。获取时间选择器的代码工作正常。我得到的时间是字符串格式,如" 18:24" 。现在我想要的是如果用户选择未来的时间,那么应该举起一个祝酒词。 例如

User Selected time from time picker="18:40"
Current Time="18:50"
then toast should be displayed

但这不会发生

代码

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            Calendar c=Calendar.getInstance();
            Date currentTime;
            String time = data.getStringExtra("TIME");
            SimpleDateFormat sdf=new SimpleDateFormat("kk:mm");
            try {
                Date timeCompare=sdf.parse(time);
                currentTime=c.getTime();
                if(timeCompare.compareTo(currentTime)==0){
                    etStartTime.setText(time);
                }else{

                    Toast.makeText(mContext, "Your cannot select future time", Toast.LENGTH_SHORT).show();
                }
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
}

2 个答案:

答案 0 :(得分:1)

行中的代码:

 Date timeCompare=sdf.parse(time);

将在1970年返回日期

代码排队:

 currentTime=c.getTime();

将在2014年返回当前日期

所以代码:

 if (timeCompare.compareTo(currentTime)==0)

永远不会执行,因为campare等于(-1 == 0)

这是一个测试代码(我在Button上创建了一个调试技术,而不是使用你的代码将它记录到logcat):

Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Calendar c=Calendar.getInstance();
                Date currentTime;
                String time = "23:00";
                SimpleDateFormat sdf=new SimpleDateFormat("kk:mm");
                try {
                    Date timeCompare=sdf.parse(time);
                    Log.e("TEST_TAG", "timeCompare is: "+ timeCompare.toString());
                    currentTime=c.getTime();
                    Log.e("TEST_TAG","currentTime is: "+ currentTime.toString());
                    Log.e("TEST_TAG", "compareTo result is: " + String.valueOf(timeCompare.compareTo(currentTime)) );
                    if(timeCompare.compareTo(currentTime)==0){
                       // setStartTime.setText(time);
                    }else{

                        Toast.makeText(getApplicationContext(), "Your cannot select future time", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });

logcat结果是:

11-14 16:18:02.618: E/TEST_TAG(2137): timeCompare is: Thu Jan 01 23:00:00 EET 1970
11-14 16:18:02.618: E/TEST_TAG(2137): currentTime is: Fri Nov 14 16:18:02 EET 2014
11-14 16:18:02.618: E/TEST_TAG(2137): compareTo result is: -1

为了纠正这个问题,我建议将数据Bandle中的时间保存为全长,例如“yyyy-MM-dd HH:mm:ss” 然后使用SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”),如下所示,您的代码将正确运行。

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date timeCompare = sdf.parse(time);
Date currentTime = new Date();
int comareResult = timeCompare.compareTo(currentTime);
if (comareResult = 0){
...
}

答案 1 :(得分:0)

您应该使用日历。当前时间也有年份和毫秒和东西...使用日历:

    Calendar c1 = Calendar.getInstance();
    c1.setTime(date1);
    Calendar c2 = Calendar.getInstance();
    c2.setTime(date2);
    c1.set(Calendar.HOUR, c2.get(Calendar.HOUR));
    c1.set(Calendar.MINUTE, c2.get(Calendar.MINUTE));
    c2.setTime(date1); //see that before it had date2 but I just wanted the hour and minute
    if (c1.compareTo(c2)...)

通过这种方式,您可以比较相同的年,月,日,分,毫米,但不同的小时和分钟。