2017-09-08 60 views
0

我想在Asettingtime和Bsettingtime當前時間
我如何比較 我改變我的時間選擇器? 什麼是最好的計劃? 使用日曆比較DATA值?時間比較與時間選擇器爲Android

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

回答

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

} 
0

您可以使用日曆對象中的值,你從TimePicker recive和其轉換爲Date對象:

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

然後讓comparisions:

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

如果你想intervall diferences見this