Java 检查ZoneDateTime是否介于特定时间之间
我用下面的代码得到一个地点的时间和日期Java 检查ZoneDateTime是否介于特定时间之间,java,zoneddatetime,Java,Zoneddatetime,我用下面的代码得到一个地点的时间和日期 ZoneId zoneId = ZoneId.of("America/New_York"); ZonedDateTime dateAndTimeForAccount = ZonedDateTime.ofInstant(now, zoneId); System.out.println(dateAndTimeForAccount); 如何检查dateAndTimeForAccount是否在早上6点到10点之间
ZoneId zoneId = ZoneId.of("America/New_York");
ZonedDateTime dateAndTimeForAccount = ZonedDateTime.ofInstant(now, zoneId);
System.out.println(dateAndTimeForAccount);
如何检查dateAndTimeForAccount
是否在早上6点到10点之间
int currentHour = dateAndTimeForAccount.getHour();
boolean isBetween6And10 = 6 <= currentHour && currentHour <= 10;
用法:
boolean介于6和10之间=dateAndTimeForAccount.query(new HourTester())代码>一种可能的解决方案是使用
注:以下解决方案将导致10:59的true(请参阅最底部的“角落案例输出”:
ZoneId zoneId = ZoneId.of("America/New_York");
ZonedDateTime dateAndTimeForAccount = ZonedDateTime.ofInstant(now, zoneId);
System.out.println(dateAndTimeForAccount);
ValueRange hourRange = ValueRange.of(8, 10);
System.out.printf("is hour (%s) in range [%s] -> %s%n",
dateAndTimeForAccount.getHour(),
hourRange,
hourRange.isValidValue(dateAndTimeForAccount.getHour())
);
示例输出
2017-01-11T07:34:26.932-05:00[America/New_York]
is hour (7) in range [8 - 10] -> false
编辑:代码段作为建议解决方案的示例指南。它不是完整的解决方案,在代码中可见,它只检查小时部分
角落案例输出:结果为10:59的true:
2017-01-11T10:59:59.999-05:00[America/New_York]
is hour (10) in range [8 - 10] -> true
介于上午6点到上午10点之间
这对于“99%”的情况应该足够了,因为“您”不能保证JVM时钟的精度超过1毫秒
return 6 <= t.getHour() && t.getHour() < 10;
在上午6点到10点之间
证明:
boolean isWithinSixToTen(ZonedDateTime t) {
return 6 <= t.getHour() && t.getHour() < 10;
}
import static org.assertj.core.api.Assertions.assertThat;
ZonedDateTime time(String time) {
return ZonedDateTime.parse("2017-01-11" + "T" + time + "-05:00[America/New_York]");
}
assertThat(isWithinSixToTen(time("10:01"))).isFalse();
assertThat(isWithinSixToTen(time("10:00"))).isFalse();
assertThat(isWithinSixToTen(time("09:59:59.999999999"))).isTrue();
assertThat(isWithinSixToTen(time("06:00"))).isTrue();
assertThat(isWithinSixToTen(time("05:59"))).isFalse();
布尔值IsWithInExtone(ZonedDateTime t){
return 6@epox我从帖子中删除了你的编辑。1)代码片段清楚地表明它只考虑了小时部分(在代码和示例输出中)2)OP接受了答案,因此似乎该代码片段足以找到他/她的解决方案。请重新访问已接受的答案。对于“10:01”-“10:59”的边缘情况,它返回TRUE,这将危及新读者,他们将把它视为“6到10”的解决方案。
static final LocalTime OPEN_TIME = LocalTime.of(06, 00);
static final LocalTime CLOSE_TIME = LocalTime.of(10, 00);
return !t.toLocalTime().isBefore(OPEN_TIME) && !t.toLocalTime().isAfter(CLOSE_TIME);
return t.toLocalTime().isAfter(OPEN_TIME) && t.toLocalTime().isBefore(CLOSE_TIME);
boolean isWithinSixToTen(ZonedDateTime t) {
return 6 <= t.getHour() && t.getHour() < 10;
}
import static org.assertj.core.api.Assertions.assertThat;
ZonedDateTime time(String time) {
return ZonedDateTime.parse("2017-01-11" + "T" + time + "-05:00[America/New_York]");
}
assertThat(isWithinSixToTen(time("10:01"))).isFalse();
assertThat(isWithinSixToTen(time("10:00"))).isFalse();
assertThat(isWithinSixToTen(time("09:59:59.999999999"))).isTrue();
assertThat(isWithinSixToTen(time("06:00"))).isTrue();
assertThat(isWithinSixToTen(time("05:59"))).isFalse();