Java Date

The class Date epresents a specific instant in time, with millisecond precision.

After Method

Returns true if the invoking Date object contains a date that is later than the one specified by date, otherwise, it returns false.

Date today = DateUtils.addDays(new Date(), 0);
Date yesterday = DateUtils.addDays(new Date(), -1);

System.out.println("today - " + today.toInstant().toString());
System.out.println("yesterday - " + yesterday.toInstant().toString());
System.out.println("today.after(yesterday) - " + today.after(yesterday));
System.out.println("today.equals(yesterday) - " + today.equals(yesterday));
System.out.println("yesterday.after(today) - " + yesterday.after(today));

// Output
today - 2020-10-02T04:34:35.465Z
yesterday - 2020-10-01T04:34:35.480Z
today.after(yesterday) - true
today.equals(yesterday) - false
yesterday.after(today) - false

 

Before Method

Returns true if the invoking Date object contains a date that is earlier than the one specified by date, otherwise, it returns false.

Date today = DateUtils.addDays(new Date(), 0);
Date yesterday = DateUtils.addDays(new Date(), -1);

System.out.println("today - " + today.toInstant().toString());
System.out.println("yesterday - " + yesterday.toInstant().toString());

System.out.println("today.before(yesterday) - " + today.before(yesterday));
System.out.println("today.equals(yesterday) - " + today.equals(yesterday));
System.out.println("yesterday.before(today) - " + yesterday.before(today));

// output
today - 2020-10-02T15:48:24.823Z
yesterday - 2020-10-01T15:48:24.837Z
today.before(yesterday) - false
today.equals(yesterday) - false
yesterday.before(today) - true

 

Check if same two dates are the same day

/**
 * Check if day1 is the same day as day2<br/>
 * day1 - 2020-6-12, day2 - 2020-6-13, return false <br/>
 * day1 - 2020-6-12, day2 - 2020-6-12, return false <br/>
 * day1 - 2020-6-13, day2 - 2020-6-12, return false <br/>
 */
public static boolean isSameDay(Date day1, Date day2) {
    if (day1 == null || day2 == null) {
        return false;
    }
    final Calendar cal1 = Calendar.getInstance();
    cal1.setTime(day1);
    final Calendar cal2 = Calendar.getInstance();
    cal2.setTime(day2);

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

 

Check if day1 is after day2

/**
 * Check if day1 is after day2<br/>
 * day1 - 2020-6-12, day2 - 2020-6-13, return false <br/>
 * day1 - 2020-6-12, day2 - 2020-6-12, return false <br/>
 * day1 - 2020-6-13, day2 - 2020-6-12, return true <br/>
 */
public static boolean isAfterDay(Date day1, Date day2) {
    if (day1 == null || day2 == null) {
        return false;
    }
    final Calendar cal1 = Calendar.getInstance();
    cal1.setTime(day1);
    final Calendar cal2 = Calendar.getInstance();
    cal2.setTime(day2);

    if (cal1.get(Calendar.YEAR) < cal2.get(Calendar.YEAR)) {
        return false;
    } else if (cal1.get(Calendar.MONTH) < cal2.get(Calendar.MONTH)) {
        return false;
    } else if (cal1.get(Calendar.DAY_OF_MONTH) <= cal2.get(Calendar.DAY_OF_MONTH)) {
        return false;
    }

    return true;
}

Check if day1 is before day2

/**
 * Check if day1 is before day2<br/>
 * day1 - 2020-6-12, day2 - 2020-6-13, return true <br/>
 * day1 - 2020-6-12, day2 - 2020-6-12, return false <br/>
 * day1 - 2020-6-13, day2 - 2020-6-12, return false <br/>
 */
public static boolean isBeforeDay(Date day1, Date day2) {
    if (day1 == null || day2 == null) {
        return false;
    }
    final Calendar cal1 = Calendar.getInstance();
    cal1.setTime(day1);
    final Calendar cal2 = Calendar.getInstance();
    cal2.setTime(day2);

    if (cal1.get(Calendar.YEAR) > cal2.get(Calendar.YEAR)) {
        return false;
    } else if (cal1.get(Calendar.MONTH) > cal2.get(Calendar.MONTH)) {
        return false;
    } else if (cal1.get(Calendar.DAY_OF_MONTH) >= cal2.get(Calendar.DAY_OF_MONTH)) {
        return false;
    }

    return true;
}

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published. Required fields are marked *