0
    Calendar calendar = Calendar.getInstance();
    Calendar cal1 = Calendar.getInstance();

    if (calendar.getTime().equals(cal1.getTime())) {
        System.out.println("Hi!");
    }

Printing calendar.getTime() and cal1.getTime() returns

Mon Aug 10 16:00:10 IST 2020
Mon Aug 10 16:00:11 IST 2020

Following code works as expected printing Hi! it should be return true for equals check but it doesn't.

    if (!calendar.getTime().after(cal1.getTime()) || !calendar.getTime().before(cal1.getTime())) {
        System.out.println("Hi!");
    }

Then why it is returning false for equals check?

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Hemlata
  • 367
  • 1
  • 3
  • 13

2 Answers2

2

Mon Aug 10 16:00:10 IST 2020 and Mon Aug 10 16:00:11 IST 2020 differ in seconds.

Also, I suggest you do not use the outdated and error-prone java.util date-time API. Use the modern date time API instead as shown below:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String dateTimeStr1 = "Mon Aug 10 16:00:10 IST 2020";
        String dateTimeStr2 = "Mon Aug 10 16:00:11 IST 2020";

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z u", Locale.ENGLISH);

        LocalDateTime date1 = LocalDateTime.parse(dateTimeStr1, formatter);
        LocalDateTime date2 = LocalDateTime.parse(dateTimeStr2, formatter);

        System.out.println(date1.equals(date2));
    }
}

Output:

false

How to compare them by excluding seconds:

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String dateTimeStr1 = "Mon Aug 10 16:00:10 IST 2020";
        String dateTimeStr2 = "Mon Aug 10 16:00:11 IST 2020";

        // Define the formatter
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z u", Locale.ENGLISH);

        // Parse the date-time strings into LocalDateTime
        LocalDateTime date1 = LocalDateTime.parse(dateTimeStr1, formatter);
        LocalDateTime date2 = LocalDateTime.parse(dateTimeStr2, formatter);

        // Create LocalDateTime instances from the parsed ones by excluding seconds
        LocalDateTime date1WithoutSec = LocalDateTime.of(date1.toLocalDate(),
                LocalTime.of(date1.getHour(), date1.getMinute()));
        LocalDateTime date2WithoutSec = LocalDateTime.of(date2.toLocalDate(),
                LocalTime.of(date2.getHour(), date2.getMinute()));

        System.out.println(date1WithoutSec.equals(date2WithoutSec));
    }
}

Output:

true
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Instead of changing API I tried this and it worked as expected. `calendar.setTimeInMillis(0);` `calendar1.setTimeInMillis(0);` – Hemlata Aug 10 '20 at 11:06
  • 1
    @Hemlata - You should stop using the poorly designed legacy date-time API. Learn the new API from **[Trail: Date Time](https://docs.oracle.com/javase/tutorial/datetime/index.html)** – Arvind Kumar Avinash Aug 10 '20 at 11:13
  • 1
    @A Thanks I'm going to switch to it. – Hemlata Aug 10 '20 at 11:14
  • An answer with a lot of good information. Your way of getting rid of the seconds is unnecessarily low level. I suggest `LocalDateTime date1WithoutSec = date1.truncatedTo(ChronoUnit.MINUTES);` (similar for date 2). – Ole V.V. Aug 11 '20 at 04:03
  • @Hemlata Your trick with `setTimeInMillis()` makes sure that the objects are *always* equal even if they were centuries apart from the outset. The confusing design of `Calendar` didn’t make this clear. With java.time it will be a lot harder to make a similar mistake. I do encourage your migration to java.time. – Ole V.V. Aug 11 '20 at 04:04
  • @OleV.V. Thanks a lot. I was checking if it is not equal then don't enable button if equals then do. I've already switched to `java.time`. – Hemlata Aug 11 '20 at 08:46
1

They are not equal at all and you can see.

Mon Aug 10 16:00:10 IST 2020

Mon Aug 10 16:00:11 IST 2020.

And your second if has or in it so it returns true

Soheil Rahsaz
  • 719
  • 7
  • 22