Here you can find the source of isBetweenDate(Date date, Date startDate, Date endDate)
Parameter | Description |
---|---|
date | a parameter |
startDate | a parameter |
endDate | a parameter |
public static boolean isBetweenDate(Date date, Date startDate, Date endDate)
//package com.java2s; /*//from w w w . j a v a 2s. co m * Copyright 2012 Eng Kam Hon (kamhon@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.Calendar; import java.util.Date; public class Main { /** * Not Consider time. Just compare date. * * @param date * @param startDate * @param endDate * @return */ public static boolean isBetweenDate(Date date, Date startDate, Date endDate) { Date clonedDate = (Date) date.clone(); clonedDate = formatDateByTime(clonedDate, 0, 0, 0, 0); Date clonedStartDate = (Date) startDate.clone(); Date clonedEndDate = (Date) endDate.clone(); clonedStartDate = formatDateByTime(clonedStartDate, 0, 0, 0, 0); clonedEndDate = formatDateByTime(clonedEndDate, 23, 59, 59, 999); return (clonedDate.equals(clonedStartDate) || clonedDate.after(clonedStartDate)) && (clonedDate.equals(clonedEndDate) || clonedDate.before(clonedEndDate)); } public static Date formatDateByTime(Date date, int hour, int minute, int second, int milisecond) { Calendar calendar = setTime(date); calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.SECOND, second); calendar.set(Calendar.MILLISECOND, milisecond); return calendar.getTime(); } public static Calendar setTime(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar; } }