Java Date Between betweenStartDateAndEndDate(Date startDate, Date endDate)

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

Description

between Start Date And End Date

License

Open Source License

Declaration

public static boolean betweenStartDateAndEndDate(Date startDate, Date endDate) 

Method Source Code

//package com.java2s;

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

public class Main {
    public static boolean betweenStartDateAndEndDate(Date startDate, Date endDate) {
        boolean bool = false;
        Date curDate = new Date();
        if ((curDate.after(startDate)) && (curDate.before(dateAdd(1, endDate, 1)))) {
            bool = true;/*from  w  w  w. j a v a2 s. c  o m*/
        }
        return bool;
    }

    public static Date dateAdd(int interval, Date date, int n) {
        long time = date.getTime() / 1000L;
        switch (interval) {
        case 1:
            time += n * 86400;
            break;
        case 2:
            time += n * 604800;
            break;
        case 3:
            time += n * 2678400;
            break;
        case 4:
            time += n * 31536000l;
            break;
        case 5:
            time += n * 3600;
            break;
        case 6:
            time += n * 60;
            break;
        case 7:
            time += n;
        }

        Date result = new Date();
        result.setTime(time * 1000L);
        return result;
    }

    public static String getTime(int format) {
        StringBuffer cTime = new StringBuffer(10);
        Calendar time = Calendar.getInstance();
        int miltime = time.get(14);
        int second = time.get(13);
        int minute = time.get(12);
        int hour = time.get(11);
        int day = time.get(5);
        int month = time.get(2) + 1;
        int year = time.get(1);
        if (format != 14)
            if (year >= 2000)
                year -= 2000;
            else
                year -= 1900;

        if (format >= 2)
            if (format == 14)
                cTime.append(year);
            else
                cTime.append(getFormatTime(year, 2));

        if (format >= 4)
            cTime.append(getFormatTime(month, 2));
        if (format >= 6)
            cTime.append(getFormatTime(day, 2));
        if (format >= 8)
            cTime.append(getFormatTime(hour, 2));
        if (format >= 10)
            cTime.append(getFormatTime(minute, 2));
        if (format >= 12)
            cTime.append(getFormatTime(second, 2));
        if (format >= 15)
            cTime.append(getFormatTime(miltime, 3));
        return cTime.toString();
    }

    private static String getFormatTime(int time, int format) {
        StringBuffer numm = new StringBuffer();
        int length = String.valueOf(time).length();
        if (format < length)
            return null;
        for (int i = 0; i < format - length; ++i)
            numm.append("0");

        numm.append(time);
        return numm.toString().trim();
    }
}

Related

  1. between(Date since, Date until, Date dateIn, Date dateOut)
  2. between(Date smdate, Date bdate)
  3. betweenDate(String from, String to)
  4. betweenHour(Date date, int hour)
  5. betweenInDay(Date date, Date start, Date end)
  6. betweenStrict(Date kezdet, Date datum, Date veg)
  7. betweenTwoDates(Date date1, Date date2)
  8. daysBetween(Date d1)
  9. daysBetween(Date d1, Date d2)