Comparing Dates

if (date1.equals(date2)) {
											System.out.println("dates are the same.");
											}
											else {
											if (date1.before(date2)) {
											System.out.println("date1 before date2");
											}
											else {
											System.out.println("date1 after date2");
											}
											}

In this phrase, we make use of the equals() and before() methods of the Date class. The equals() method will return true if the data values are the same; otherwise it will return false. The dates must be the same down to the millisecond in order for the equals() method to return true. The before() method returns true if the date on which it is called occurs before the date passed as a parameter to this method.

The Date class also has an after() method, which is used similarly to the before() method to determine if the date on which ...

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.