This example demonstrate Date
‘s class after()
method to check if a date is later than another date.
package org.kodejava.util;
import java.util.Date;
import java.util.Calendar;
public class DateCompareAfter {
public static void main(String[] args) {
// Get current date
Date today = new Date();
// Add 1 day from the current date.
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
Date tomorrow = calendar.getTime();
// Tests if this date is after the specified date. This method will
// return true if the value time represented by the tomorrow object
// is later than today.
if (tomorrow.after(today)) {
System.out.println(tomorrow + " is after " + today);
}
}
}
The result of the code snippet above is:
Tue Oct 05 20:52:34 CST 2021 is after Mon Oct 04 20:52:33 CST 2021
Latest posts by Wayan (see all)
- How do I build simple search page using ZK and Spring Boot? - March 8, 2023
- How do I calculate days between two dates excluding weekends and holidays? - January 12, 2023
- How do I discover the quarter of a given date? - January 11, 2023