Here you can find the source of compare(Date startDate, Date endDate, Date targetDate)
Parameter | Description |
---|---|
startDate | range start date |
endDate | range end date |
targetDate | target date |
public static int compare(Date startDate, Date endDate, Date targetDate)
//package com.java2s; /*/*from www .j a va 2 s . com*/ * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the * Free Software Foundation; either version 2.1 of the License, or (at your * option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ import java.util.Calendar; import java.util.Date; public class Main { /** * This method compares a target date with a date range. The method will * return 0 if the date is within the range, less than zero if the date * is before the range starts, and greater than zero if the date is after * the range ends. * * @param startDate range start date * @param endDate range end date * @param targetDate target date * @return comparison result */ public static int compare(Date startDate, Date endDate, Date targetDate) { return (compare(startDate, endDate, targetDate.getTime())); } /** * This method compares a target date with a date range. The method will * return 0 if the date is within the range, less than zero if the date * is before the range starts, and greater than zero if the date is after * the range ends. * * @param startDate range start date * @param endDate range end date * @param targetDate target date in milliseconds * @return comparison result */ public static int compare(Date startDate, Date endDate, long targetDate) { int result = 0; if (targetDate < startDate.getTime()) { result = -1; } else { if (targetDate > endDate.getTime()) { result = 1; } } return (result); } /** * Compare two dates, handling null values. * * @param d1 Date instance * @param d2 Date instance * @return int comparison result */ public static int compare(Date d1, Date d2) { int result; if (d1 == null || d2 == null) { result = (d1 == d2 ? 0 : (d1 == null ? 1 : -1)); } else { long diff = d1.getTime() - d2.getTime(); result = ((diff == 0) ? 0 : ((diff > 0) ? 1 : -1)); } return (result); } /** * Create a Date instance representing a specific time. * * @param hour hour 0-23 * @param minutes minutes 0-59 * @return new Date instance */ public static Date getTime(int hour, int minutes) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.MINUTE, minutes); cal.set(Calendar.SECOND, 0); return (cal.getTime()); } }