You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从Combo Box获取的Date对象与已有对象用==比较不匹配问题

问题排查与解决
  • 核心问题不在Combo Box的取值逻辑,而是对象比较方式错误:Java里==对引用类型(比如Date)比较的是内存地址,不是内容。你新创建的consultationDate和appointmentList里的Date是两个不同实例,哪怕日期内容完全一致,内存地址也不一样,所以==永远不成立。
  • 修复方法:用equals()方法替代==来比较Date对象的内容:
    if (appointmentList.get(0).getDate().equals(consultationDate)) {
        sopl("Working");
    }
    
  • 额外提示:你使用的Date(int day, int month, int year)构造方法已被废弃,推荐改用Java 8及以上的LocalDate类(更安全、易用),比如:
    LocalDate consultationDate = LocalDate.of(apptYear, apptMonth, apptDay);
    // 比较时同样用equals
    if (appointmentList.get(0).getLocalDate().equals(consultationDate)) {
        sopl("Working");
    }
    

内容的提问来源于stack exchange,提问作者Hassan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 18:15:38