Java - Write code to calculate minutes Difference between two dates

Requirements

Write code to calculate minutes Difference between two dates

Demo

import java.util.Calendar;
import java.util.Date;

public class Main {
  public static void main(String[] argv) {
    Date start = new Date();
    Date end = new Date();
    System.out.println(minutesDiff(start, end));
  }/*from w  w  w .java2  s  .  co  m*/

  public static long minutesDiff(Date start, Date end) {
    long day = compareDate(start, end);
    if (day == 0) {
      return 0;
    }
    if (day > 0) {
      return (day - 1) / 1000 / 60 + 1;
    } else {
      return (day + 1) / 1000 / 60 - 1;
    }

  }

  public static long compareDate(Date start, Date end) {
    long temp = 0;
    Calendar starts = Calendar.getInstance();
    Calendar ends = Calendar.getInstance();
    starts.setTime(start);
    ends.setTime(end);
    temp = ends.getTime().getTime() - starts.getTime().getTime();
    return temp;
  }
}

Related Exercise