时间与Android的时间选择器进行比较

时间:2017-09-08 06:55:58

标签: android timepicker

我希望Asettingtime和Bsettingtime在当前时间之间 我怎么比较 我改变了我的时间选择器? 什么是最好的计划? 使用日历比较DATA值?

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

2 个答案:

答案 0 :(得分:0)

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date strDate = sdf.parse(date2);
if (yourdate1 > strDate.getTime()) {

}

答案 1 :(得分:0)

您可以使用日历对象设置从TimePicker中重新获取的值并将它们转换为日期对象:

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

然后进行比较:

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

如果您想要intervall差异,请参阅this