Java Date Compare compareTimesOfDay(Date time1, Date time2)

Here you can find the source of compareTimesOfDay(Date time1, Date time2)

Description

Calculates difference in milliseconds taking into account only time of day.

License

Open Source License

Parameter

Parameter Description
time1 First Date containing time of day.
time2 Second Date containing time of day.

Return

Difference in milliseconds taking into account time of day, time1 - time2.

Declaration

public static long compareTimesOfDay(Date time1, Date time2) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**//from  ww w .j  av  a 2  s.  co m
     * Calculates difference in milliseconds taking into account only time of day.
     * @param time1 First Date containing time of day. 
     * @param time2 Second Date containing time of day.
     * @return Difference in milliseconds taking into account time of day, time1 - time2.
     */
    public static long compareTimesOfDay(Date time1, Date time2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(time1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(time2);

        long difference = 0;

        difference = (cal1.get(Calendar.HOUR_OF_DAY) - cal2.get(Calendar.HOUR_OF_DAY)) * 60 * 60 * 1000;
        difference += (cal1.get(Calendar.MINUTE) - cal2.get(Calendar.MINUTE)) * 60 * 1000;
        difference += (cal1.get(Calendar.SECOND) - cal2.get(Calendar.SECOND)) * 1000;
        difference += cal1.get(Calendar.MILLISECOND) - cal2.get(Calendar.MILLISECOND);

        return difference;
    }
}

Related

  1. compareMonth(Date first, Date second)
  2. compareSecond(java.util.Date date1, java.util.Date date2)
  3. compareStartDateEndDate(String startDate, String endDate)
  4. compareTime(Date date1, Date date2)
  5. compareTime(Date first, Date second)
  6. compareTo(Date src, Date dest)
  7. compareToCal(Date sDate, Date eDate)
  8. compareToMonthByDate(Date startDate, Date endDate)
  9. compareTwoDate(Date beforeDate, Date afterDate)