我正在使用下面的代码来检查一个小时是否介于另外两个特定时间之间:
String openHour = "08:00 AM";
String currentHour = "10:00 PM";
String closeHour = "11:00 PM"; //Change to 02:00 AM doesn't work!!!
SimpleDateFormat format = new SimpleDateFormat("hh:mm a");
Date openHourDate = format.parse(openHour);
Date currentHourDate = format.parse(currentHour);
Date closeHourDate = format.parse(closeHour);
Calendar openCalendar = Calendar.getInstance();
openCalendar.setTime(openHourDate);
Calendar currentCalendar = Calendar.getInstance();
currentCalendar.setTime(currentHourDate);
Calendar closeCalendar = Calendar.getInstance();
closeCalendar.setTime(closeHourDate);
Date open = openCalendar.getTime();
Date current = currentCalendar.getTime();
Date close = closeCalendar.getTime();
if (current.after(open) && current.before(close)) {
System.out.println("Correct!");
} else {
System.out.println("Incorrect!");
}
如果currentHour
就像"10:00 PM"
您在我的代码中看到的那样,一切正常,但如果我将其更改为"02:00 AM"
,即使currentHour
介于08:00 AM
和之间,代码也不会按预期工作02:00 AM
。如何解决这个问题?