After and before methods in Java date class

The after method returns true if this date is a later time than the when argument. Otherwise, it returns false. The before method returns true if this date is before the specified date and returns false otherwise.

The following code prints “date1 before date2” because the first date represents a time that is one millisecond earlier than the second.

Date date1 = new Date(1000);
Date date2 = new Date(1001);
if (date1.before(date2)) {
    System.out.println("date1 before date2");
} else {
    System.out.println("date1 not before date2");
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.