How to compare Dates in Java

In this tutorial, JavaSampleApproach will show you ways to compare two Dates in Java 8.

I. Methods to use
1. Is Checking methods

Each of these methods return a boolean.

//LocalDate date1 = ...;
date1.isBefore(LocalDate date2);
date1.isAfter(LocalDate date2);
date1.isEqual(LocalDate date2);
2. Compare method

This method returns a negative value if less, positive value if greater, zero if equal.

//LocalDate date1 = ...;
date1.compareTo(LocalDate date2);
II. Practice

This example compares two java.time.LocalDate:

package com.ozenero.comparedate;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class MainApp {

	public static void main(String[] args) {

		DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd-MM-yyyy");
		System.out.println("=== Date Info (day-month-year) ===");
		LocalDate date1 = LocalDate.of(2017, 11, 01);
		LocalDate date2 = LocalDate.of(2018, 01, 1);

		System.out.println("date1 : " + dateFormat.format(date1));
		System.out.println("date2 : " + dateFormat.format(date2));

		System.out.println("=== Is Checking ===");
		System.out.println(date1 + " is before " + date2 + " --> " + date1.isBefore(date2));
		System.out.println(date1 + " is after " + date2 + " --> " + date1.isAfter(date2));
		System.out.println(date1 + " is equal " + date2 + " --> " + date1.isEqual(date2));

		System.out.println("=== Compare ===");
		System.out.println("date1.compareTo(date2) -> " + date1.compareTo(date2));
		System.out.println("date2.compareTo(date1) -> " + date2.compareTo(date1));


Run the code above and check results:

=== Date Info (day-month-year) ===
date1 : 01-11-2017
date2 : 01-01-2018
=== Is Checking ===
2017-11-01 is before 2018-01-01 --> true
2017-11-01 is after 2018-01-01 --> false
2017-11-01 is equal 2018-01-01 --> false
=== Compare ===
date1.compareTo(date2) -> -1
date2.compareTo(date1) -> 1

42 thoughts on “How to compare Dates in Java”

  1. 66939 34830Hiya! Amazing blog! I happen to be a day-to-day visitor to your website (somewhat a lot more like addict ) of this site. Just wanted to say I appreciate your blogs and am seeking forward for far more to come! 154809

  2. We absolutely love your blog and find nearly all of your post’s to be precisely what I’m looking for. Would you offer guest writers to write content to suit your needs? I wouldn’t mind producing a post or elaborating on many of the subjects you write about here. Again, awesome web log!

  3. Great – I should definitely say I’m impressed with your blog. I had no trouble navigating through all the tabs and related info. It ended up being truly simple to access. Great job…

  4. Youre so cool! I dont suppose Ive read anything similar to this prior to. So nice to get somebody with many original thoughts on this subject. realy appreciate beginning this up. this website is one area that is needed on the internet, a person after some originality. beneficial task for bringing a new challenge for the net!

  5. Whats Going down i’m new to this, I stumbled upon this I have discovered It absolutely helpful and it has aided me out loads. I hope to contribute & assist other customers like its aided me. Great job.

  6. Thank you for the sensible critique. Me and my neighbor were just preparing to do some research on this. We got a grab a book from our local library but I think I learned more from this post. I am very glad to see such great information being shared freely out there.

  7. 33752 935589More than and more than once more I like to take into consideration this difficulties. As a matter of fact it wasnt even a month ago that I thought about this quite thing. To be honest, what may be the answer though? 230071

  8. I have been surfing on-line more than 3 hours nowadays, yet I never found any attention-grabbing article like yours. It’s lovely value sufficient for me. In my view, if all webmasters and bloggers made excellent content as you did, the net can be a lot more helpful than ever before.

  9. What i do not realize is in fact how you are now not really much more smartly-preferred than you might be right now. You are so intelligent. You know thus considerably in terms of this subject, made me individually consider it from a lot of various angles. Its like women and men don’t seem to be fascinated until it is one thing to accomplish with Girl gaga! Your own stuffs outstanding. All the time deal with it up!

  10. Undeniably believe that which you stated. Your favorite justification appeared to be on the web the easiest thing to be aware of. I say to you, I certainly get annoyed while people consider worries that they just do not know about. You managed to hit the nail upon the top and also defined out the whole thing without having side effect , people can take a signal. Will likely be back to get more. Thanks

  11. Have you ever thought about writing an ebook or guest authoring on other sites? I have a blog centered on the same subjects you discuss and would really like to have you share some stories/information. I know my readers would enjoy your work. If you’re even remotely interested, feel free to shoot me an email.

  12. The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.

  13. After study a few of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls check out my web site as well and let me know what you think.

  14. Throughout the awesome design of things you’ll get a B- just for hard work. Where you misplaced me was first in all the facts. You know, as the maxim goes, details make or break the argument.. And it couldn’t be more true right here. Having said that, allow me reveal to you just what exactly did work. The article (parts of it) is definitely very engaging and that is possibly the reason why I am making the effort in order to comment. I do not really make it a regular habit of doing that. Second, although I can see a jumps in reasoning you make, I am definitely not certain of how you appear to connect the details which in turn make the conclusion. For now I will, no doubt subscribe to your point but hope in the near future you actually link the facts much better.

  15. Nice post. I learn something more challenging on different blogs everyday. It will always be stimulating to read content from other writers and practice a little something from their store. I’d prefer to use some with the content on my blog whether you don’t mind. Natually I’ll give you a link on your web blog. Thanks for sharing.

  16. What i do not understood is actually how you’re not really much more well-liked than you might be now. You’re very intelligent. You realize thus considerably relating to this subject, made me personally consider it from so many varied angles. Its like men and women aren’t fascinated unless it’s one thing to do with Lady gaga! Your own stuffs excellent. Always maintain it up!

  17. Very good site you have here but I was wanting to know if you knew of any forums that cover the same topics talked about here? I’d really love to be a part of online community where I can get opinions from other knowledgeable people that share the same interest. If you have any suggestions, please let me know. Appreciate it!

  18. Nice post. I was checking constantly this blog and I’m impressed! Extremely helpful info specially the last part 🙂 I care for such information much. I was looking for this certain info for a very long time. Thank you and good luck.

  19. Greetings! I know this is kinda off topic but I was wondering which blog platform are you using for this website? I’m getting tired of WordPress because I’ve had problems with hackers and I’m looking at options for another platform. I would be fantastic if you could point me in the direction of a good platform.

  20. I have not checked in here for a while because I thought it was getting boring, but the last few posts are great quality so I guess I’ll add you back to my daily bloglist. You deserve it my friend 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *