How to calculate the time difference between two time fields , with respect to the date changes

后端 未结 1 1236
无人及你
无人及你 2020-12-15 13:09

I want to calculate the time difference.I have three EditTexts , I want to input the times in the first two edittexts in HH:MM format. And then calculate the time difference

相关标签:
1条回答
  • 2020-12-15 14:09

    Time Difference is calculated by following code:

    long difference = date2.getTime() - date1.getTime(); 
    days = (int) (difference / (1000*60*60*24));  
    hours = (int) ((difference - (1000*60*60*24*days)) / (1000*60*60)); 
    min = (int) (difference - (1000*60*60*24*days) - (1000*60*60*hours)) / (1000*60);
    

    EDIT :

    date1 and date2 are Date objects, if you have EditText then you can convert your text in to Date object by using following code,

      DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); // Make sure user insert date into edittext in this format.
         Date dateObject;
    
      try{
        String dob=(tx.getText().toString());
        dateObject = formatter.parse(dob);
        date = new SimpleDateFormat("dd/MM/yyyy").format(dateObject);
    
        }
    
    0 讨论(0)
提交回复
热议问题