calendar.getTime().equals(cal1) return false though both calendars has same time

huangapple go评论74阅读模式
英文:

calendar.getTime().equals(cal1) return false though both calendars has same time

问题

以下是您要翻译的代码部分:

        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?
英文:
    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?

答案1

得分: 2

Mon Aug 10 16:00:10 IST 2020Mon Aug 10 16:00:11 IST 2020 在秒数上不同。

此外,我建议您不要使用过时且容易出错的 java.util 日期时间 API。而是使用 现代日期时间 API,如下所示:

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));
    }
}

输出:

false

如何排除秒数进行比较:

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";

        // 定义格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z u", Locale.ENGLISH);

        // 将日期时间字符串解析为 LocalDateTime
        LocalDateTime date1 = LocalDateTime.parse(dateTimeStr1, formatter);
        LocalDateTime date2 = LocalDateTime.parse(dateTimeStr2, formatter);

        // 通过排除秒数从解析的日期时间创建 LocalDateTime 实例
        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));
    }
}

输出:

true
英文:

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

答案2

得分: 1

它们完全不相等,你可以看到。

Mon Aug 10 16:00:10 IST 2020

Mon Aug 10 16:00:11 IST 2020.

而且你的第二个if语句中有or,所以它返回true

英文:

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

huangapple
  • 本文由 发表于 2020年8月10日 18:36:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63338527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定