Java Yesterday isYesterday(Date date)

Here you can find the source of isYesterday(Date date)

Description

is Yesterday

License

Open Source License

Declaration

public static boolean isYesterday(Date date) 

Method Source Code


//package com.java2s;
/*/*from  w  ww  . j  ava2  s .c  o m*/
 * Copyright (C) 2005 - 2014 TIBCO Software Inc. All rights reserved.
 * http://www.jaspersoft.com.
 *
 * Unless you have purchased  a commercial license agreement from Jaspersoft,
 * the following license terms  apply:
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License  as
 * published by the Free Software Foundation, either version 3 of  the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero  General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public  License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    public static boolean isYesterday(Date date) {
        return isSameDay(date, rollDay(new Date(), -1));
    }

    public static boolean isSameDay(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            throw new IllegalArgumentException("The dates must not be null");
        }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA)
                && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
                && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
    }

    private static Date rollDay(Date date, long amount) {
        long millisecondsInDay = 24 * 60 * 60 * 1000;
        long rolledTime = date.getTime() + amount * millisecondsInDay;
        return new Date(rolledTime);
    }
}

Related

  1. getYesterdayFormat(String format)
  2. getYesterdayFormatString(String format)
  3. getYesterdaysDate()
  4. getYesterdayStartDate()
  5. getYesterdayString()
  6. yesterday()
  7. yesterday()
  8. yesterday()
  9. yesterDay()