2016-04-22 70 views
0

我已經安裝了timePicker24 hour模式,但是如果時間目前16:05我選擇16:10timePicker,它說的12 hours and 5 minutes代替5 minutes。我如何輕鬆更換AMPMtimePicker上午和下午交換並帶來了負面分鐘數

另外,如果我選擇相同的hourminutes小於current minutes,我會得到一個輸出,如12 hours and -4 minutes

代碼:

public void onClickAlarmOn(View v) { 

    final Calendar c = Calendar.getInstance(); 
    TimePicker alarm_time_picker = (TimePicker) findViewById(R.id.timePicker); //initializing timePicker before using it //http://stackoverflow.com/questions/25485768/why-does-my-app-keep-crashing-when-i-try-to-insert-a-date-and-time-picker 
    int hour = alarm_time_picker.getCurrentHour(); //get selected hour 
    int minute = alarm_time_picker.getCurrentMinute(); //get selected minute 

    int hour_now = c.get(Calendar.HOUR); //get system's hour 
    int minute_now = c.get(Calendar.MINUTE); //get system's minute 

    int hour_result = hour - hour_now; //subtract the time selected by time.now of system 
    int minute_result = minute - minute_now; 

    String hour_result_string = String.valueOf(hour_result); //convert to string to display 
    String minute_result_string = String.valueOf(minute_result); 

    setToast_result("Alarm set to " + hour_result_string + " hours " + minute_result_string + " minutes"); 
} 

謝謝!

+0

是c#還是java?它不可能是兩個,可以嗎? – user1666620

+0

@ user1666620對不起,困惑了一秒,修正了。 – hurkaperpa

回答

0

如果您使用的是24小時制時鐘,則您希望從Calendar實例獲取Calendar.HOUR_OF_DAY,而不是基於12小時制的Calendar.HOUR

int hour_now = c.get(Calendar.HOUR_OF_DAY); 
相關問題