Java Day End isBetweenDate(Date date, Date startDate, Date endDate)

Here you can find the source of isBetweenDate(Date date, Date startDate, Date endDate)

Description

Not Consider time.

License

Apache License

Parameter

Parameter Description
date a parameter
startDate a parameter
endDate a parameter

Declaration

public static boolean isBetweenDate(Date date, Date startDate, Date endDate) 

Method Source Code

//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;
    }
}

Related

  1. getTrendTime(Date date)
  2. getWorkingDays(Calendar start, Calendar end, final Long[] holidays)
  3. getWorkingDaysBetweenTwoDates(Date startDate, Date endDate)
  4. getYearsBetween(Date startDate, Date endDate)
  5. getYearsBetweenDate(Date begin, Date end)
  6. isBetWeenDates(Date in, Date from, Date to)
  7. isCalenderDaySame(GregorianCalendar aInCal1, GregorianCalendar aInCal2)
  8. isEndOfMonth(Date d)
  9. isEndOfMonth(Date date)