如何查找请求的时间是否在 Java 中的时间范围之间

Jav*_*SSE 1 java datetime date java-time

我有字符串格式的日期输入(例如:-)2020-01-08T07:00:00,我想检查输入时间是否在6:00到之间15:00。知道如何检查吗?

我试过下面的代码:-

java.util.Date inputDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse("2020-01-08T07:00:00");
if(inputDate.getTime() < "06:00" && inputDate.getTime() > "15:00") 
Run Code Online (Sandbox Code Playgroud)

但它显然无法与 String 进行比较,所以我很困惑如何比较它?

YCF*_*F_L 5

请避免使用旧的日期库,我建议这样使用java.time

// format your date time by using the default formater of LocalDateTime
LocalDateTime ldt = LocalDateTime.parse("2020-01-08T07:00:00");

// Get the time part from your LocalDateTime
LocalTime lt = ldt.toLocalTime();

// create a patter to format the two times
DateTimeFormatter hmFormatter = DateTimeFormatter.ofPattern("H:mm");

// use isAfter and isBefore to check if your date in the range you expect
if (lt.isAfter(LocalTime.parse("6:00", hmFormatter)) && 
        lt.isBefore(LocalTime.parse("15:00", hmFormatter))) {

}
Run Code Online (Sandbox Code Playgroud)