Java Date Before beforeDay(final Date date1, final Date date2)

Here you can find the source of beforeDay(final Date date1, final Date date2)

Description

Get date one day before specified one.

License

Open Source License

Parameter

Parameter Description
date1 test date
date2 date when

Return

true if date1 is before date2

Declaration

public static boolean beforeDay(final Date date1, final Date date2) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**//from   w  w  w  .j  a  v  a2  s  . c  o  m
     * milliseconds in a second.
     */
    public static final long SECOND = 1000;
    /**
     * milliseconds in a minute.
     */
    public static final long MINUTE = SECOND * 60;

    /**
     * Get date one day before specified one.
     * 
     * @param date1
     *            test date
     * @param date2
     *            date when
     * @return true if date1 is before date2
     */
    public static boolean beforeDay(final Date date1, final Date date2) {
        if (date1 == null)
            return true;
        return getStartOfDate(date1).before(getStartOfDate(date2));
    }

    /**
     * Get start of date.
     * 
     * @param date
     * @see Date
     * @return the start of date
     */
    public static Date getStartOfDate(final Date date) {
        if (date == null)
            return null;
        Calendar cal = GregorianCalendar.getInstance();
        cal.setTime(date);

        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        return new Date(cal.getTime().getTime());
    }
}

Related

  1. before(Date point, Date when)
  2. before(Date sDate, Date eDate)
  3. beforeDays(Date date, Integer days)
  4. beforeTime(Date time1, Date time2)
  5. daysBefore(Date date1)
  6. getBefore(Date date)