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

Java中LocalTime的before/after方法报错:找不到符号问题求助

解决LocalTime比较时的"cannot find symbol"错误

嘿,我一眼就瞅出问题所在了!你当前代码里的result.format(formatter)会把LocalTime对象转换成String类型,但String类压根没有before()和after()方法——这就是编译器抛出"cannot find symbol"错误的核心原因。

你要做的是直接比较时间对象本身,而不是格式化后的字符串。result本身就是LocalTime类型的实例,直接用它调用before()和after()方法就行,完全没必要先格式化:

LocalTime calc = LocalTime.parse("06:00", formatter);
LocalTime calc2 = LocalTime.parse("17:30", formatter);

// 直接用LocalTime对象进行时间比较,无需格式化
boolean before = result.before(calc);
boolean after = result.after(calc2);

这样修改后,你就是在两个LocalTime实例之间做时间先后的合法性比较,完全符合Java时间API的设计逻辑,编译器的错误自然就消失了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:29:45