Finding the difference between a time and the current time <p>So I'm writing an app that has similar functionality to an alarm clock. It asks for the user to input a time. But I've been stuck on how to figure this out for a while.</p> <p>The method needs to find the difference in minutes between the user selected time, and the current time. Assuming that the user will always put in a time that is AFTER the current time (otherwise it wouldn't make sense to use my app), the difference would just be (userTimeInMins - currTimeInMins), where both are calculated by ((hours * 60) + minutes).</p> <h2>BUT. Here's the problem:</h2> <p>Ex 1) If the current time is 10 PM, and the user enters in the time 2 AM. The above algorithm would say that the difference between the two times is (22 * 60 + 0) - (2 * 60 + 0), which is clearly incorrect because this would mean there is a difference of 20 hours between 10PM and 2 AM, when the difference is actually 4 hours. <br/> <br/> Ex 2) If the current time is 1PM, and the user enters in the time 2AM. The above algorithm would say that the difference between the two times is (13 * 60 + 0) - (2 * 60 + 0), which is again incorrect because this would mean there is a difference of 11 hours, when the difference is actually 13 hours.</p> <h2>What I have so far</h2> <p>I've realized that for example 1 and for example 2, the difference in minutes can be calculated with (((24 + userHours) * 60) + userMinutes) - currTimeInMins</p> <p>I'm struggling to come up with decision statements in the method to determine whether to use the first method or the second method to calculate the difference in minutes.</p> <h2>The Code</h2> <pre><code>// Listener for the time selection TimePickerDialog.OnTimeSetListener time = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { String currAM_PM = ""; String userAM_PM = ""; // Get user AM/PM int hourToShow = 0; if (hourOfDay &gt; 12){ hourToShow = hourOfDay - 12; userAM_PM = "PM" } else if (hourOfDay == 12){ hourToShow = hourOfDay; userAM_PM = "PM" } else{ hourToShow = hourOfDay; userAM_PM = "AM" } // Update the time field to show the selected time in 12-hr format EditText timeField = (EditText) findViewById(R.id.editTime); timeField.setText(hourToShow + ":" + minute + " " + userAM_PM); // Get current hour SimpleDateFormat sdf = new SimpleDateFormat("HH"); String cHour = sdf.format(new Date()); int currHour = Integer.parseInt(cHour); // Get current AM/PM if (currHour &gt; 12){ currAM_PM = "PM" } else if (currHour == 12){ currAM_PM = "PM" } else{ currAM_PM = "AM" } // Calculate the time to use // If the selected hour is less than the current hour AND am_pm is "AM" // THIS IS THE WHERE I NEED HELP //-------------------------------------------------------------- if(currAM_PM == "PM" &amp;&amp; userAM_PM == "AM" .... ??????) { //take 24, add the hour, use same minute. so that 3 am is 27:00. timeToUse = ((24 + hourOfDay) * 60) + minute; } else timeToUse = (hourOfDay * 60) + minute; } // timeToUse is then passed through an intent extra to the next activity // where the difference between it and the current time is calculated and used // for other purposes. }; </code></pre> <h2>Thanks for any help ahead of time.</h2>