1

I'm trying to get the first Thursday of the month

dates.forEach(date -> {
        System.out.println("Date: " + date);
        LocalDate dateT = date.with(firstInMonth(DayOfWeek.THURSDAY));
        System.out.println("First Thursday: " + dateT);
        if(date == dateT) {
            System.out.println("Date: " + date + " is the first thursday");
        }
        System.out.println("");
    });

dates is a hashset of LocalDate objects, 7 days for mon - sun They objects may be across two months (31st May, 1st June etc.)

The output of the prints is as expected, but the if is never triggered. I have the exact sae code running for the first Tuesday which runs fine.

It successfully picks up that the first Thursday of June is 2021-06-03, but the print in the if never triggers

Date: 2021-05-31
First Thursday: 2021-05-06

Date: 2021-06-06
First Thursday: 2021-06-03

Date: 2021-06-05
First Thursday: 2021-06-03

Date: 2021-06-04
First Thursday: 2021-06-03

Date: 2021-06-03
First Thursday: 2021-06-03

Date: 2021-06-02
First Thursday: 2021-06-03

Date: 2021-06-01
First Thursday: 2021-06-03
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Ceri Turner
  • 830
  • 2
  • 12
  • 36
  • 2
    Does this answer your question? [Compare two objects with .equals() and == operator](https://stackoverflow.com/questions/13387742/compare-two-objects-with-equals-and-operator) – Ole V.V. May 22 '21 at 07:18
  • From [the documentation of `LocalDate`](https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html): *This is a value-based class; use of identity-sensitive operations (including reference equality (`==`), identity hash code, or synchronization) on instances of `LocalDate` may have unpredictable results and should be avoided. The `equals` method should be used for comparisons.* – Ole V.V. May 22 '21 at 07:21

3 Answers3

1
dates.forEach(date -> {
        System.out.println("Date: " + date);
        LocalDate dateT = date.with(firstInMonth(DayOfWeek.THURSDAY));
        System.out.println("First Thursday: " + dateT);
        if(date.equals(dateT)) {
            System.out.println("Date: " + date + " is the first thursday");
        }
        System.out.println("");
    });

use equals() instead of == as you are comparing the object not two values.

Note : In Java, the == operator compares that two references are identical or not. Whereas the equals() method compares two objects.

Rohith V
  • 1,089
  • 1
  • 9
  • 23
0

== tests that two variables are the exact same object. You should be using equals to test for equality between two different objects:

if (date.equals(dateT)) {
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

You are trying to compare two objects and not the values they are holding.

For LocalDates specifically you can use isEqual

dates.forEach(date -> {
        System.out.println("Date: " + date);
        LocalDate dateT = date.with(firstInMonth(DayOfWeek.THURSDAY));
        System.out.println("First Thursday: " + dateT);
        if(date.isEqual(dateT)) {
            System.out.println("Date: " + date + " is the first thursday");
        }
        System.out.println("");
    });

ozerodb
  • 543
  • 3
  • 13