JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

How to compare two dates in Java 8?


Java 8 provides isBefore(), isAfter() and isEquals() methods on LocalDate, LocalTime and LocalDateTime classes. These methods will be helpful to compare date objects.

In Java 8 date comparision are much easy compare to earlier versions.

CompareDateEx
package com.java2novice.datetime;

import java.time.LocalDate;

public class CompareDateEx {

	public static void main(String[] args) {

		// create custom date objects
		LocalDate dtObj1 = LocalDate.of(2015, 12, 15);
        LocalDate dtObj2 = LocalDate.of(2016, 01, 15);

        System.out.println("First date: "+dtObj1);
        System.out.println("Second date: "+dtObj2);

        boolean result = dtObj1.isAfter(dtObj2);
        System.out.println("is first date after second date?: "+result);

        result = dtObj2.isAfter(dtObj1);
        System.out.println("is second date after first date?: "+result);

        result = dtObj1.isBefore(dtObj2);
        System.out.println("is first date before second date?: "+result);

        // you can use equals  method on date objects
        result = dtObj1.isEqual(dtObj2);
        System.out.println("is first date equals to second date?: "+result);

        // compare two dates with comareTo method
        if(dtObj1.compareTo(dtObj2) > 0) {
        		System.out.println("first date is after second date");
        } else if(dtObj1.compareTo(dtObj2) < 0) {
        		System.out.println("first date is before second date");
        } else if(dtObj1.compareTo(dtObj2) == 0) {
        		System.out.println("first date is equals to second date");
        }
	}
}

Output:
First date: 2015-12-15
Second date: 2016-01-15
is first date after second date?: false
is second date after first date?: true
is first date before second date?: true
is first date equals to second date?: false
first date is before second date
<< Previous Program | Next Program >>

Java-8 Date and Time API Examples

  1. Java 8 LocalDateTime example.
  2. Java 8 ZonedDateTime example.
  3. Java 8 LocalDate example.
  4. Java 8 LocalTime example.
  5. Create date time with custom values in Java 8.
  6. Java 8 Instant (Timestamp) example.
  7. Java 8 Period example.
  8. Java 8 Duration example.
  9. Java 8 Date parsing and formatting example.
  10. How to compare two dates in Java 8?
  11. How to get current date and time Java 8?
  12. How to calculate execution / elapsed time Java 8?
  13. How to convert string to Date Java 8?
  14. How to add/substract days to Date in Java 8?
Knowledge Centre
wait Vs sleep methods
sleep(): It is a static method on Thread class. It makes the current thread into the "Not Runnable" state for specified amount of time. During this time, the thread keeps the lock (monitors) it has acquired.

wait(): It is a method on Object class. It makes the current thread into the "Not Runnable" state. Wait is called on a object, not a thread. Before calling wait() method, the object should be synchronized, means the object should be inside synchronized block. The call to wait() releases the acquired lock.
Famous Quotations
Be yourself; everyone else is already taken.
-- Oscar Wilde

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.