2015-06-19 74 views
3

我目前正在嘗試將一定量的時間添加到日曆對象。輸入來自時間選擇器,輸出轉到兩個字符串。向日歷對象添加分鐘

DateFormat dateFormat = new android.text.format.DateFormat(); 

    Calendar startTimeCalendar = Calendar.getInstance(); 

    //These two string will be like this: "08:15AM" 
    int prevStartHour = Integer.parseInt(startTime.getText().subSequence(0, 2).toString()); 
    int prevStartMinute = Integer.parseInt(startTime.getText().subSequence(3, 5).toString()); 
    int prevEndHour = Integer.parseInt(endTime.getText().subSequence(0, 2).toString()); 
    int prevEndMinute = Integer.parseInt(endTime.getText().subSequence(3, 5).toString()); 

    //Get the difference in hours between two strings 
    int hourChange = Math.abs(prevEndHour - prevStartHour); 
    //Get the difference in minutes between two strings 
    int minuteChange = Math.abs(prevEndMinute - prevStartMinute); 

    //Set the date on the first label 
    startTime.setText(dateFormat.format("hh:mmAA", startTimeCalendar).toString()); 
    //Add the difference in hours to the calendar 
    startTimeCalendar.add(Calendar.HOUR_OF_DAY, hourChange); 
    //Add the difference in minutes to the calendar 
    startTimeCalendar.add(Calendar.MINUTE, minuteChange); 
    //Set the date on the second label 
    endTime.setText(dateFormat.format("hh:mmAA", startTimeCalendar).toString()); 

現在,這是奇怪的部分......小時只是罰款和花花公子,而分鐘只是決定中途停止工作。而不是增加預期時間(15,30或45分鐘),每增加30分鐘。

+0

也許你的prevEndMinute和prevStartMinute不是你除了之外的數字。 –

+1

您是否嘗試過調試並檢查'hourChange'和'minuteChange'的值? – christopher

+0

我相信兩者之間的區別是正確的。我記錄了他們,他們正常流動(在我輸入他們的差異15,30和45分鐘) –

回答