简体   繁体   中英

Time compare with time picker for android

i want between Asettingtime and Bsettingtime in current time
how can i compare i change my timepicker? what is best plan? use calender compare DATA value?

dpd=new TimePickerDialog(SettingActivity.this,
                                new TimePickerDialog.OnTimeSetListener() {
                                    @Override

                                    public void onTimeSet(TimePicker view,
                                                          int hourOfDay, int minute) {
                                        Toast.makeText(getApplicationContext(),

                                            hourOfDay +"시 " + minute+"분 을 선택했습니다",
                                            Toast.LENGTH_SHORT).show();

                                    String sHour;
                                    if(hourOfDay < 10){
                                        sHour = "0"+hourOfDay;
                                    } else {
                                        sHour = String.valueOf(hourOfDay);
                                    }
                                    int sTime;
                                    String sMinute = "00";
                                    if(minute < 10){
                                        sMinute = "0"+minute;
                                    } else {
                                        sMinute = String.valueOf(minute);
                                    }
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date strDate = sdf.parse(date2);
if (yourdate1 > strDate.getTime()) {

}

You can use the Calendar Object for setting the values that you recive from the TimePicker and the convert them to Date Object:

    Calendar calendar1 = Calendar.getInstance();
    calendar1.set(Calendar.YEAR, year);
    calendar1.set(Calendar.MONTH, month);
    calendar1.set(Calendar.DAY_OF_MONTH, day);
    calendar1.set(Calendar.HOUR_OF_DAY, hour);
    calendar1.set(Calendar.MINUTE, minute);

    Date date = new Date(calendar1.getTimeInMillis());

    Calendar calendar2 = Calendar.getInstance();
    calendar2.set(Calendar.YEAR, year);
    calendar2.set(Calendar.MONTH, month);
    calendar2.set(Calendar.DAY_OF_MONTH, day);
    calendar2.set(Calendar.HOUR_OF_DAY, hour);
    calendar2.set(Calendar.MINUTE, minute);

    Date date2 = new Date(calendar2.getTimeInMillis());

And then make comparisions:

        date1.after(date2);
        //or
        date1.before(date2);
        //or
        date1.compareTo(date2);

If you want intervall diferences see this :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM