How to find if requested time is between timerange in Java

JavaSSE :

I have the date input in the string format (ex:- 2020-01-08T07:00:00) and i want to check if input time is between 6:00 to 15:00. any idea how can i check this?

I have tried below code:-

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

but it can not compare with String obviously,so i am confused how to compare it?

YCF_L :

Please avoid using legacy date library, instead I suggest to use java.time like this :

// 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))) {

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=193649&siteId=1