Calculate Difference between two times in Android

后端 未结 7 2124
-上瘾入骨i
-上瘾入骨i 2020-11-27 21:47

I have two string variables such as StartTime and EndTime. I need to Calculate the TotalTime by subtracting the EndTime with StartTime.

The Format of StartTime and E

7条回答
  •  无人及你
    2020-11-27 22:09

    Try simple piece of code using For 24 hour time
    StartTime = "10:00";
    EndTime = "13:00";
    here starthour=10 and end hour=13 
    if(TextUtils.isEmpty(txtDate.getText().toString())||TextUtils.isEmpty(txtDate1.getText().toString())||TextUtils.isEmpty(txtTime.getText().toString())||TextUtils.isEmpty(txtTime1.getText().toString()))
        {
            Toast.makeText(getApplicationContext(), "Date/Time fields cannot be blank", Toast.LENGTH_SHORT).show();
        }
        else {
            if (starthour > endhour) {
                Toast.makeText(getApplicationContext(), "Start Time Should Be Less Than End Time", Toast.LENGTH_SHORT).show();
            } else if (starthour == endhour) {
                if (startmin > endmin) {
                    Toast.makeText(getApplicationContext(), "Start Time Should Be Less Than End Time", Toast.LENGTH_SHORT).show();
                }
                else{
                    tvalid = "True";
                }
            } else {
                // Toast.makeText(getApplicationContext(),"Sucess"+(endhour-starthour)+(endmin-startmin),Toast.LENGTH_SHORT).show();
                tvalid = "True";
            }
        }
    same for date also
    

提交回复
热议问题