Search code examples
androidandroid-timepicker

Compare time in android


I am making an app in which i require user to select a time from time picker.The code to get the time picker works fine.I get the time in a string format like "18:24".Now what i want is if user select future time then a toast should be raised. For example

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

But this is not happening

Code

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();
            }
}

Solution

  • the code in line:

     Date timeCompare=sdf.parse(time);
    

    will return a date in 1970

    while the code in line:

     currentTime=c.getTime();
    

    will return the current date in 2014

    so the code:

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

    will never executed because the campare is equal to (-1 == 0)

    here is a test code (I created on Button as a debugging techniques, instead use your code to log it out to 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();
                    }
    
                }
            });
    

    the logcat result is:

    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
    

    to correct this, I suggest to save the time in data Bandle as full length like "yyyy-MM-dd HH:mm:ss" then use SimpleDateFormat("yyyy-MM-dd HH:mm:ss") as following so your code will run correctly.

    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){
    ...
    }