-1

I am currently working with streams in Java. I am creating a list which stores table values as an object from MySQL.

I want to filter the contents by comparing with a year in the filter. However, the date column in the table has the format dd-mm-yyy. When I try to compare by using getYear() with my input year, for example 2018, it is showing an wrong output (I know getYear() has been deprecated).

How to compare two dates using streams which is in the format dd-mm-yyy?

Below contains the code snippet I am working with:

public List<Payment> findCustomerByYear(int year){
    List<Payment> list1 = paymentDao.getAllRecord();
    List<Payment> list2 = new ArrayList<>();

    list2 = list1.stream()
                 .filter(i -> i.getPaymentDate().getYear() == year)
                 .collect(Collectors.toList());
    return list2;
}

Is there any other option to compare dates instead of using getYear() in list2?

  • 5
    What is reported error and what does `getPaymentDate()` return? – Tom Mar 17 '21 at 14:19
  • Convert to a ZonedDateTime and call getYear(): https://docs.oracle.com/javase/9/docs/api/java/time/ZonedDateTime.html – keuleJ Mar 17 '21 at 14:22
  • btw `List list2 = new ArrayList<>();` is wasteful as it assigns a newly created ArrayList which is then immediately thrown away. – k314159 Mar 17 '21 at 14:33
  • @Tom. It showing getYear() was deprecated. getPaymentDate() is just a getter method to retrieve date for that particular object – Sampath Atmuri Mar 17 '21 at 14:37
  • @keuleJ. It contains more than one object and all the comparisons need to done in that stream only. Can you give me a example ? – Sampath Atmuri Mar 17 '21 at 14:39
  • getYear() being deprecated is not an error. It's a warning. I suspect your error is that you are comparing a value such as 121 returned from getYear(), with 2021. The documentation for getYear() describes what it does. – k314159 Mar 17 '21 at 14:41
  • 1
    @k314159. Yes it was returning 121. Then how to convert into full year. – Sampath Atmuri Mar 17 '21 at 14:43
  • @k314159. list2 = list1.stream().filter(i->(i.getPaymentDate().getYear()+1900)==year).collect(Collectors.toList()); is it correct now ? – Sampath Atmuri Mar 17 '21 at 14:47
  • 2
    Yes that looks correct. Although, as getPaymentDate() returns a Date, which is a legacy type, it may be worth the effort to change it to return a modern replacement such as LocalDate or ZonedDateTime. – k314159 Mar 17 '21 at 15:02
  • 1
    @k314159. Thank you. Sure i will change it – Sampath Atmuri Mar 17 '21 at 15:05
  • 1
    I recommend you don’t use `Date` (no matter if that was `java.util.Date` or `java.sql.Date`). Those classes are poorly designed and long outdated. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). You can directly get a `LocalDate` from MySQL, see [Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2](https://stackoverflow.com/questions/43039614/insert-fetch-java-time-localdate-objects-to-from-an-sql-database-such-as-h2). – Ole V.V. Mar 17 '21 at 15:14

1 Answers1

3

A DATE column in MySQL has no “format” because it is not text.

You should fix whatever tooling is retrieving data from the database for export to a text file. That tooling is creating the text in that format. That tooling should instead be using the YYYY-MM-DD format defined by both the SQL standard and the ISO 8601 standard.

If fixing that tooling is not feasible, use DateTimeFormatter with a custom formatting pattern to match your text, to produce LocalDate objects. This has been covered many many times already on Stack Overflow. Search to learn more.

Never use the java.sql.Date and java.until.Date classes. These terrible classes are now legacy, supplanted years ago by the modern java.time classes defined in JSR 310.

enter image description here

For more info, see Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154