But i couldn't able to sort the date in descending order.

Two easy options:

  • You could just reverse your comparison yourself, using secondDate.compareTo(firstDate). (I assume that in your real code you're actually returning retVal; it's ignored in your posted code.)
  • Call Collections.reverseOrder(Comparator) to create a comparator with the reverse order of the original one.
Answer from Jon Skeet on Stack Overflow
🌐
JavaTechOnline
javatechonline.com › home › java 8 comparator comparing date descending
Java 8 Comparator Comparing Date Descending Archives - JavaTechOnline
Home > Posts tagged "java 8 comparator comparing date descending" java · Core Java · Java 8 by devs5003 - January 24, 2024July 16, 20240 · Sorting a List by Date sometimes becomes a bit tricky. One of the tricky part is how to handle date format and then perform the sorting.
🌐
Oreate AI
oreateai.com › blog › mastering-descending-order-a-deep-dive-into-javas-comparator-and-comparingdate › b0b13d77802a4667e14d9b5840c0b50b
Mastering Descending Order: A Deep Dive Into Java's Comparator and `comparingDate` - Oreate AI Blog
1 week ago - For descending order, if date A is later than date B, our comparator should tell the sorting algorithm that A comes before B. Java 8 introduced some incredibly handy static methods on the Comparator interface, and Comparator.comparing() is a ...
🌐
Baeldung
baeldung.com › home › java › guide to java comparator.comparing()
Guide to Java Comparator.comparing() | Baeldung
January 8, 2024 - This article is a guide to several features introduced in Java 8 for the Comparator interface.
🌐
Benchresources
benchresources.net › home › java › java 8 – how to sort list by java.util.date in 4 ways ?
Java 8 - How to Sort List by java.util.Date in 4 ways ? - BenchResources.Net
February 2, 2023 - We will create separate class implementing java.util.Comparator interface and overriding compare() method for ascending-order sorting · For descending-order sorting, we will reverse the Comparator using reversed() method · Finally, we will print product list to console in ascending and descending ...
🌐
JavaTechOnline
javatechonline.com › home › comparator.comparing date descending
Comparator.comparing Date Descending Archives - JavaTechOnline
One of the tricky part is how to handle date format and then perform the sorting. In this article 'How to Sort List by Date in Java 8 ?', we will discuss sorting of three types of Dates. These are java.util.Date, java.time.LocalDate and java.time.LocalDateTime.
🌐
GeeksforGeeks
geeksforgeeks.org › java › compare-dates-in-java
Compare Dates in Java - GeeksforGeeks
July 11, 2025 - import java.io.*; import ...println(calendar1.after(calendar2)); } } ... Method - 4 : Using Java 8 isBefore(), isAfter(), isEqual() and compareTo() methods: In Java 8, the isBefore(), isAfter(), isEqual() and compareTo() ...
🌐
Baeldung
baeldung.com › home › java › java dates › sorting objects in a list by date
Sorting Objects in a List by Date | Baeldung
April 3, 2025 - This will reverse the comparison and thus return the result in descending order: @Test public void givenEmpList_SortEmpList_thenCheckSortedListDescV1() { Collections.sort(employees, new Comparator<Employee>() { public int compare(Employee emp1, Employee emp2) { return emp2.getJoiningDate().compareTo(emp1.getJoiningDate()); } }); assertEquals(employees, employeesSortedByDateDesc); } We can also convert the above method to more concise forms using the Java 8 Lambda Expressions.
🌐
JavaTechOnline
javatechonline.com › home › how to sort list by date in java 8 ?
How To Sort List By Date In Java 8 ? - Java Sort By Date
March 31, 2025 - In order to get list in descending order we have two solutions. 1) Use reverse() method of Collections class 2) Modify Comparator for descending comparing order
Find elsewhere
🌐
Medium
medium.com › @AlexanderObregon › javas-comparator-comparing-method-explained-342361288af6
Java’s Comparator.comparing() Method Explained | Medium
October 2, 2024 - The Comparator.comparing() method is a highly flexible tool for sorting collections in Java. It simplifies custom sorting based on object properties, making code more readable and maintainable. By understanding how to use comparing() for sorting by various fields like name, date, or price, and considering performance factors such as key extraction and chaining comparators, developers can write efficient and flexible sorting logic.
🌐
Sebastian Daschner
blog.sebastian-daschner.com › entries › java_8_comparator
Java 8 Comparator - Sebastian Daschner
Since Java 8, you can also define new Comparators Builder-pattern like: Comparator .comparing(Task::isFinished) .thenComparing(Comparator .comparing(Task::getPriority, Comparator.nullsFirst(Integer::compareTo)).reversed()) .thenComparing(Comparator .comparing(Task::getUpdated, Comparator.nullsFirst(Date::compareTo)).reversed()) .thenComparing(Task::getName); The several order definitions are chained and contain lambda expressions (or just method references as you can see).
🌐
Coderanch
coderanch.com › t › 401051 › java › Date-Ascending-order-Comparator
Date Ascending order Comparator (Beginning Java forum at Coderanch)
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... I have a comparator that sorts a collection of objects by date in descending order, but I'm having problem creating a comparator that sorts by date in ascending order.
🌐
CalliCoder
callicoder.com › how-to-compare-date-time-java
How to compare Date and Time in Java | CalliCoder
February 18, 2022 - import java.time.LocalDateTime; public class CompareLocalDateTimeExample { public static void main(String[] args) { LocalDateTime date1 = LocalDateTime.now(); LocalDateTime date2 = LocalDateTime.of(2020, 1, 31, 10, 15, 45); // isAfter() method if(date1.isAfter(date2)) { System.out.println(date1 + " is after " + date2); } // isBefore() method if(date1.isBefore(date2)) { System.out.println(date1 + " is before " + date2); } // isEqual() method if(date1.isEqual(date2)) { System.out.println(date1 + " is equal to " + date2); } // compareTo() method int diff = date1.compareTo(date2); if(diff > 0) { System.out.println(date1 + " is greater than " + date2); } else if (diff < 0) { System.out.println(date1 + " is less than " + date2); } else { System.out.println(date1 + " is equal to " + date2); } } }
🌐
Stack Overflow
stackoverflow.com › questions › 21482078 › compare-dates-using-comparator-in-java
android - Compare dates using comparator in Java - Stack Overflow
A simple TreeSet (assuming you have distinct dates) using a Comparator comparing the dateTime.getMillis() of each date for instance would do the trick. And better, why not use directly this "out-of-the-box" Comparator ;) Up to you to implement the exact logic you want. (ascendant, descendant, whatever)
🌐
Medium
medium.com › @AlexanderObregon › javas-comparator-reverseorder-method-explained-9f9b8bebd87b
Java’s Comparator.reverseOrder() Method Explained | Medium
January 26, 2025 - By combining the natural ordering logic of the Comparable interface with Comparator.reverseOrder(), you can sort objects in descending order based on a field, such as scores or dates.
🌐
How to do in Java
howtodoinjava.com › home › java date time › compare two dates in java
Compare dates in Java 7 & Java 8 - HowToDoInJava
April 4, 2023 - Learn to compare two given dates in Java to find out which date is earlier and which is later in the universal timeline. We will see date comparison examples using the following classes: LocalDate, LocalDateTime and ZonedDateTime classes from Java 8
🌐
Blogger
javarevisited.blogspot.com › 2021 › 09 › comparator-comparing-thenComparing-example-java-.html
Java 8 Comparator comparing() and thenComparing() Example - Tutorial
I like books, so I'll create a Book object with some fields to demonstrate how you can sort a list of books using Java 8 features like lambda expressions and method reference. ... public class Book implements Comparable < Book > { private String title; private String author; private int price; public Book(String title, String author, int price) { this.title = title; this.author = author; this.price = price; } public String getTitle() { return title; } public String getAuthor() { return author; } public int getPrice() { return price; } public void setTitle(String title) { this.title = title; }
🌐
Baeldung
baeldung.com › home › java › java dates › comparing dates in java
Comparing Dates in Java | Baeldung
July 17, 2024 - Since the release of Java 8, it is recommended to migrate to the Java 8 Date/Time API. Similarly to LocalDate and LocalDateTime, both java.util.Date and java.util.Calendar objects have after(), before(), compareTo() and equals() methods for comparing two date instances.