O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required