How to calculate the time difference between two time fields, regarding a date change

I want to calculate the time difference. I have three EditTexts, I want to enter the time in the first two edittexts in the format HH: MM. And then calculate the time difference, and the result will be displayed on the third edittext field in the same format. If the date changes, the time difference will be calculated in accordance with this, i.e.

If the first time = 23:00 and the second time = 01:00

then time difference = 02:00 hours

public class TimeCalculate extends Activity { private String mBlock; private String mBlockoff; private String mBlockon ; // String mHours, mMinutes; Date date1, date2; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); EditText blockoff = (EditText) findViewById(R.id.blockoff); mBlockoff = blockoff.getText().toString(); EditText blockon = (EditText) findViewById(R.id.blockon); mBlockon = blockon.getText().toString(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm"); try { date1 = simpleDateFormat.parse(mBlockoff); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { date2 = simpleDateFormat.parse(mBlockon); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } mBlock = getDifference(date1, date2); EditText block = (EditText) findViewById(R.id.block); block.setText(mBlock.toString()); } public static String getDifference(Date startTime, Date endTime) { if (startTime == null) return "corrupted"; Calendar startDateTime = Calendar.getInstance(); startDateTime.setTime(startTime); Calendar endDateTime = Calendar.getInstance(); endDateTime.setTime(endTime); long milliseconds1 = startDateTime.getTimeInMillis(); long milliseconds2 = endDateTime.getTimeInMillis(); long diff = milliseconds2 - milliseconds1; /*int hours = (int)diff / (60 * 60 * 1000); int minutes = (int) (diff / (60 * 1000)); minutes = minutes - 60 * hours; long seconds = diff / (1000); */ //timeDiff = DateUtils.formatElapsedTime(seconds); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:MM"); Date date = new Date(diff); return simpleDateFormat.format(date); } } 

I have executed this code but it gives an error because the source was not found. I think the error is in the getDifference method. Please give any other logic

+6
android
source share
1 answer

The time difference is calculated using the 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 an EditText , then you can convert your text to a Date object using the 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); } 
+28
source share

All Articles