Here you can find the source of compareTimesOfDay(Date time1, Date time2)
Parameter | Description |
---|---|
time1 | First Date containing time of day. |
time2 | Second Date containing time of day. |
public static long compareTimesOfDay(Date time1, Date time2)
//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; } }