How to Get Minutes Difference Between Two Time in Android

Calculate Difference between two times in Android

Try below code.

// suppose time format is into ("hh:mm a") format

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a");

date1 = simpleDateFormat.parse("08:00 AM");
date2 = simpleDateFormat.parse("04:00 PM");

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);
hours = (hours < 0 ? -hours : hours);
Log.i("======= Hours"," :: "+hours);

Output - Hours :: 8

Time difference between two times

To Calculate the difference between two dates you could try something like:

long millis = date1.getTime() - date2.getTime();
int hours = (int) (millis / (1000 * 60 * 60));
int mins = (int) ((millis / (1000 * 60)) % 60);

String diff = hours + ":" + mins;

To update the Time Difference every second you can make use of Timer.

Timer updateTimer = new Timer();
updateTimer.schedule(new TimerTask() {
public void run() {
try {
long mills = date1.getTime() - date2.getTime();
int hours = millis/(1000 * 60 * 60);
int mins = (mills/(1000*60)) % 60;

String diff = hours + ":" + mins; // updated value every1 second
} catch (Exception e) {
e.printStackTrace();
}
}

}, 0, 1000); // here 1000 means 1000 mills i.e. 1 second

Edit : Working Code :

public class MainActivity extends Activity {

private TextView txtCurrentTime;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtCurrentTime= (TextView)findViewById(R.id.mytext);
Timer updateTimer = new Timer();
updateTimer.schedule(new TimerTask()
{
public void run()
{
try
{

SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
Date date1 = format.parse("08:00:12 pm");
Date date2 = format.parse("05:30:12 pm");
long mills = date1.getTime() - date2.getTime();
Log.v("Data1", ""+date1.getTime());
Log.v("Data2", ""+date2.getTime());
int hours = (int) (mills/(1000 * 60 * 60));
int mins = (int) (mills/(1000*60)) % 60;

String diff = hours + ":" + mins; // updated value every1 second
txtCurrentTime.setText(diff);
}
catch (Exception e)
{
e.printStackTrace();
}
}

}, 0, 1000);
}

Calculate minutes between two dates

First convert date to milliseconds and then subtract and convert back to minutes.

for eg,


long startDateMillis = startDate.getTime();
long endDateMillis = endDate.getTime();

long diffMillis = startDateMillis - endDateMillis;

long minutes = (diffMillis/1000)/60;

How to calculate difference between two times in Java?

You could take two dates/times and minus them from each other to get a duration.
This duration can then be converted into whatever format you need.

The example is an example, your mileage may vary.

LocalDateTime fromDateTime = LocalDateTime.of(2019, 10, 07, 7, 45, 55);
LocalDateTime current = LocalDateTime.now();
long duration = current.toEpochSecond(ZoneOffset.UTC) - fromDateTime.toEpochSecond(ZoneOffset.UTC);
double minutes = duration/60.0;
double hours = duration/3600.0;
System.out.println("Minutes: "+ minutes);
System.out.println("Hours: "+ hours);

Difference between two Times in Minutes are coming as negative

As there is no date used, you have to check first if your endTime is less than your startTime. If yes, then your endTime is on the next day and you have to add 1 day/86 400 000 milliseconds. Then you will have your desired result.

Just add this condition:

if(endTime.getTime() < startTime.getTime()){  

long mills = ((endTime.getTime() + 86400000) - startTime.getTime()); 1 day = 86 400 000 mill

}

Hope this helps

How do I get difference between two dates in android?, tried every thing and post

You're close to the right answer, you are getting the difference in milliseconds between those two dates, but when you attempt to construct a date out of that difference, it is assuming you want to create a new Date object with that difference value as its epoch time. If you're looking for a time in hours, then you would simply need to do some basic arithmetic on that diff to get the different time parts.

Java:

long diff = date1.getTime() - date2.getTime();
long seconds = diff / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;

Kotlin:

val diff: Long = date1.getTime() - date2.getTime()
val seconds = diff / 1000
val minutes = seconds / 60
val hours = minutes / 60
val days = hours / 24

All of this math will simply do integer arithmetic, so it will truncate any decimal points



Related Topics



Leave a reply



Submit