Abhi
Abhi

Reputation: 1574

Check if TimePicker time is less than current time

I'm trying to set time from a TimePickerdialog in a TextView after checking if the set time is past the current time. I take the time that is selected from the TimePicker in a string s and parsed it in (hh:mm) format.I used SimpleDateFormat.parse("hh:mm") but it compares the time in the year 1970 and today's date and time.

So I tried SimpleDateFormat("dd-mm-yy hh:mm:ss.SSS").parse(s); along with the date,month and year but it is giving me ParseException. I use the before() to check if the time selected is before the current time. But I should parse the time that is set along with date and time first to use the method. How to check if the time set in TimePicker is the less than current time.

This is what I tried:

public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        Calendar datetime = Calendar.getInstance();
        Calendar c = Calendar.getInstance();
        datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
        datetime.set(Calendar.MINUTE, minute);
        String s = c.get(Calendar.DAY_OF_WEEK) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.YEAR) + " " + String.valueOf((datetime.get(Calendar.HOUR_OF_DAY)) + ":" + datetime.get(Calendar.MINUTE) + ":" + datetime.get(Calendar.SECOND));
        try {
            Date d = new SimpleDateFormat("dd-mm-yy hh:mm:ss.SSS").parse(s);
            Date date = c.getTime();
            if (d.before(c.getTime())) {
                DateTimePicker.setBooleanDateCheckForTimePicker(false);
                Toast.makeText(getActivity(), "Enter valid time", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (datetime.get(Calendar.AM_PM) == Calendar.AM)
            am_pm = "AM";
        else if (datetime.get(Calendar.AM_PM) == Calendar.PM)
            am_pm = "PM";

        String strHrsToShow = (datetime.get(Calendar.HOUR) == 0) ? "12" : datetime.get(Calendar.HOUR) + "";
        boolean b = new DateTimePicker().booleanDateCheckForTimePicker;
        if (new DateTimePicker().booleanDateCheckForTimePicker) {
            timePicker.setText(strHrsToShow + ":" + datetime.get(Calendar.MINUTE) + " " + am_pm);
        }


        //timePicker.setText(hourOfDay + " "+minute);
    }
}

Upvotes: 1

Views: 7746

Answers (2)

tiny sunlight
tiny sunlight

Reputation: 6251

Just compare timestamp.

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        Calendar datetime = Calendar.getInstance();
        Calendar c = Calendar.getInstance();
        datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
        datetime.set(Calendar.MINUTE, minute);
        if(datetime.getTimeInMillis() > c.getTimeInMillis()){
//            it's after current
        }else{
//            it's before current'
        }
    }

Upvotes: 9

Oubai Abbasi
Oubai Abbasi

Reputation: 111

Instead of doing the comparison using Date you can just check it manually like this:

var isBefore=false;

if(hourOfDay < datetime.get(Calendar.HOUR_OF_DAY))
  isBefore=true;
else if(hourOfDay == datetime.get(Calendar.HOUR_OF_DAY)){
  if(minute < datetime.get(Calendar.MINUTE))
    isBefore=true;
}

Upvotes: 3

Related Questions