检查 ZonedDateTime 是否在某个时间之间

Checking if a ZonedDateTime is between a certain time

我正在使用以下代码获取某个位置的时间和日期

        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;

如果您想针对任何特定的 java.time class 概括它,您可以使用 TemporalQuery class:

class HourTester extends TemporalQuery<Boolean> {
    @Override
    public Boolean queryFrom(TemporalAccessor temporal) {
        int hour = temporal.get(ChronoField.HOUR_OF_DAY);
        return 6 <= hour && hour <= 10;
    }
}

用法:boolean isBetween6And10 = dateAndTimeForAccount.query(new HourTester());

一种可能的解决方案是使用 ValueRange.

注意:以下解决方案将为 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

edit:片段指南作为建议解决方案的示例。这不是一个完整的解决方案,如代码所示,它只检查小时部分。

极端案例输出: 导致 true for 10:59:

2017-01-11T10:59:59.999-05:00[America/New_York]
is hour (10) in range [8 - 10] -> true

介于 6 am10 am 之间

这对于 "99%" 的情况应该足够了,因为 "you" 不能保证 JVM 时钟的精度超过 1 毫秒.

    return 6 <= t.getHour() && t.getHour() < 10;

介于6 to 10 am之间(含

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

介于6 to 10 am之间独占

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