Get the minutes difference in Java

Description

The following code shows how to get the minutes difference.

Example


//  w  w w.  j av a 2  s  .co  m
import java.util.Calendar;
import java.util.Date;

public class Main {
  /**
   * Get the minutes difference
   */
  public final static long SECOND_MILLIS = 1000;
  public final static long MINUTE_MILLIS = SECOND_MILLIS * 60;

  public static int minutesDiff(Date earlierDate, Date laterDate) {
    if (earlierDate == null || laterDate == null)
      return 0;

    return (int) ((laterDate.getTime() / MINUTE_MILLIS) - (earlierDate
        .getTime() / MINUTE_MILLIS));
  }

  public static void main(String[] argv) {
    Date date1 = Calendar.getInstance().getTime();
    Calendar cal = Calendar.getInstance();
    cal.set(2010, 10, 10);
    Date date2 = cal.getTime();
    System.out.println(minutesDiff(date1, date2));
  }

}

The code above generates the following result.





















Home »
  Java Tutorial »
    Date »




Date Get
Date Set
Date Format
Date Compare
Date Convert
Date Calculation
Date Parse
Timezone