Java Date Value Check isDateInRange(Date toCheck, Date minRange, Date maxRange)

Here you can find the source of isDateInRange(Date toCheck, Date minRange, Date maxRange)

Description

is Date In Range

License

Open Source License

Declaration

public static boolean isDateInRange(Date toCheck, Date minRange,
            Date maxRange) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    public static boolean isDateInRange(Date toCheck, Date minRange,
            Date maxRange) {/* w  ww.java  2s.c  o m*/
        String toCheckStr = formatYmd(toCheck);
        return ((minRange == null || toCheckStr
                .compareTo(formatYmd(minRange)) >= 0) && (maxRange == null || toCheckStr
                .compareTo(formatYmd(maxRange)) <= 0));
    }

    /**
     * Utility method to format a date in yyyy-MM-dd format
     * @param d the date to format
     * @return a String representing the date in the passed format
     */
    public static String formatYmd(Date d) {
        return formatDate(d, "yyyy-MM-dd", "");
    }

    /**
     * Utility method to format a date in the given format
     * @param d the date to format
     * @return a String representing the date in the passed format
     */
    public static String formatDate(Date d, String format) {
        return formatDate(d, format, "");
    }

    /**
     * Utility method to format a date in the given format
     * @param d the date to format
     * @param format the DateFormat to use
     * @param defaultIfNUll the value to return if the passed date is null
     * @return a String representing the date in the passed format
     */
    public static String formatDate(Date d, String format,
            String defaultIfNull) {
        if (d != null) {
            DateFormat df = new SimpleDateFormat(format);
            return df.format(d);
        }
        return defaultIfNull;
    }
}

Related

  1. isDateFormat(String dateString)
  2. isDateFormat(String str, String format)
  3. isDateFormatString(String s, String dateFormat, Locale locale)
  4. isDateFormatValid(final String pattern)
  5. isDateInRange(Date startDt, Date endDt, Date theDate)
  6. IsDateOK(String date)
  7. isDateSortedAsc(List dates, String format)
  8. isDateTwo(String value)
  9. isDateValid(String date, Locale locale)