Java Date Format Check checkDateRange(String fromDate, String endDate)

Here you can find the source of checkDateRange(String fromDate, String endDate)

Description

Check between date range.

License

Open Source License

Parameter

Parameter Description
fromDate the from date
endDate the end date

Return

true, if successful

Declaration

public static boolean checkDateRange(String fromDate, String endDate) 

Method Source Code

//package com.java2s;
/**// w ww.j av a 2  s  .  com
 * Copyright (c) 2014 Far Eastone Telecommunications Co., Ltd.
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information of 
 * Far Eastone Telecommunications Co., Ltd. ("Confidential Information"). 
 * 
 * You shall not disclose such Confidential Information and shall use it 
 * only in accordance with the terms of license agreement you entered
 * into with Far Eastone Telecommunications Co., Ltd.
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.Locale;

public class Main {
    /**
     * Check between date range.
     *
     * @param fromDate the from date
     * @param endDate the end date
     * @return true, if successful
     */
    public static boolean checkDateRange(String fromDate, String endDate) {
        long fromTime = transDate(fromDate, "yyyy-MM-dd").getTime();
        long endTime = transDate(endDate, "yyyy-MM-dd").getTime();
        long todayTime = transDate(getDate(), "yyyy-MM-dd").getTime();

        if (fromTime <= todayTime && todayTime <= endTime) {
            return true;
        }
        return false;
    }

    /**
     * Transform date string to Date object.
     *
     * @param date the date
     * @param date_format the date_format
     * @return Date
     */
    public static Date transDate(String date, String date_format) {
        try {
            Date s_date = new SimpleDateFormat(date_format).parse(date);
            return s_date;
        } catch (ParseException e) {
            //         e.printStackTrace();
        }
        return null;
    }

    /**
     * Get current date.
     * 
     * @return string
     */
    public static String getDate() {
        return _datetime("yyyy-MM-dd");
    }

    /**
     * Assign return data format.
     *
     * @param format the format
     * @return string
     */
    private static String _datetime(String format) {
        return _datetime(format, new Date());
    }

    /**
     * Assign return data format.
     *
     * @param format the format
     * @param date the date
     * @return string
     */
    private static String _datetime(String format, Date date) {
        SimpleDateFormat formater = new SimpleDateFormat(format);
        return formater.format(date).toString();
    }

    /**
     * Assign return data format (by Locale).
     *
     * @param format the format
     * @param date the date
     * @param locale the locale
     * @return string
     */
    private static String _datetime(String format, Date date, Locale locale) {
        SimpleDateFormat formater = new SimpleDateFormat(format, locale);
        return formater.format(date).toString();
    }
}

Related

  1. checkDateFormat()
  2. checkDateFormat(String pattern)
  3. checkDateFormat(String strTime, String pattern)
  4. checkDateFormatAndValite(String strDateTime, String format)
  5. checkDateInToday(Date date)
  6. checkDateValidity(String str, String formatString)
  7. checkTime(int id)
  8. checkTimestamp(String timestamp)