Java Date Compare isAfterOrEqual(Date date, Date toCompare)

Here you can find the source of isAfterOrEqual(Date date, Date toCompare)

Description

Returns true if toCompare is after or on same day as date.

License

Open Source License

Parameter

Parameter Description
date a parameter
toCompare a parameter

Declaration

public static boolean isAfterOrEqual(Date date, Date toCompare) 

Method Source Code


//package com.java2s;
/*/* w  w  w . j  a va 2 s .co  m*/
 * Author Stephen Booysen
 *
 * Copyright (c) 2015 Stephen Booysen, Inc. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Stephen Booysen. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Stephen Booysen
 *
 * Stephen Booysen MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

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

import java.util.Date;

public class Main {
    /**
     * Returns true if toCompare is after or on same day as date. it only compares yyyy-MM-dd and will assume HH:mm:ss to be 00:00:00
     * 
     * @param date
     * @param toCompare
     * @return
     */
    public static boolean isAfterOrEqual(Date date, Date toCompare) {
        if (date == null || toCompare == null) {
            return false;
        }
        date = zeroTime(date);
        toCompare = zeroTime(toCompare);
        return toCompare.compareTo(date) <= 0;
    }

    /**
     * sets all the time related fields to ZERO!
     *
     * @param date
     *
     * @return Date with hours, minutes, seconds and ms set to ZERO!
     */
    public static Date zeroTime(final Date date) {
        DateFormat format = new SimpleDateFormat("dd.MM.yyyy");
        Date trimmed = null;
        try {
            trimmed = format.parse(format.format(date));
        } catch (ParseException e) {
        } // will never happen
        return trimmed;
    }

    /**
     * Try and evaluate the string and return a date
     * 
     * @param input
     * @return
     */
    public static Date parse(String input) {

        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("yyyy-MM-dd").parse(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("yyyy/MM/dd").parse(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("dd MMM yyyy").parse(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy").parse(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy").parse(input);
        } catch (Exception e) {

        }
        try {
            return new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").parse(input);
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * Try to evaluate a date and return a string
     * 
     * @param input
     * @return
     */
    public static String parse(Date input) {

        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(input);
        } catch (Exception e) {
        }

        return null;
    }

    /**
     * Try to evaluate a date and return a string
     * 
     * @param input
     * @return
     */
    public static String parse(Date input, String format) {

        try {
            return new SimpleDateFormat(format).format(input);

        } catch (Exception e) {
        }

        return null;
    }
}

Related

  1. getCompareDate(Date startDate, Date endDate)
  2. getCompareDate(String strDate1, String strDate2, String pFormat)
  3. getCompareDate(String strDate1, String strDate2, String pFormat)
  4. getCompareDateNum(String sDateValue1, String sDateValue2)
  5. getCompareResult(String date1, String date2)
  6. isBeforeDate(String sCompareDate)
  7. isInDate(Date date, Date compareDate)
  8. max(Collection datumok)
  9. max(Date a, Date b)