我确实想检查 Instant 是否在其他两个瞬间之间:
目前我使用:
import java.time.format.DateTimeFormatter;
import java.time.Instant;
Instant start = Instant.from(DateTimeFormatter.ISO_DATE_TIME.parse("2016-10-25T12:31:39.084726218Z"));
Instant end = Instant.from(DateTimeFormatter.ISO_DATE_TIME.parse("2016-10-25T13:31:39.084726218Z"));
// for exclusive range
Instant testSubject1 = Instant.from(DateTimeFormatter.ISO_DATE_TIME.parse("2016-10-25T12:31:40Z"));
boolean isInRange1 = testSubject1.isAfter(start) && testSubject1.isBefore(end); // this works as exclusive range
//for inclusive range
Instant testSubject2 = Instant.from(DateTimeFormatter.ISO_DATE_TIME.parse("2016-10-25T12:31:39.084726218Z"));
boolean isInRange2 = (testSubject2.equals(start) || testSubject2.isAfter(start)) && (testSubject2.equals(end) || testSubject2.isBefore(end)); // inclusive range
是否有任何其他实用函数是标准库或其他地方允许这种范围检查的简化方法?
我正在寻找类似的东西:
new InstantRange(start,end).checkInstantWithin(testSubject1);
// or
InstantUtils.inRangeExclusive(start,end, testSubject1);
InstantUtils.inRangeInclusivestart,end, testSubject1);
最佳答案
您可以在 ThreeTen-Extra 中使用 Interval
对于这样的任务。 (假设您愿意拉入图书馆)
关于java - 是否有一个 java 类代表两个瞬间之间的范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58730007/