1

我正在使用下面的代码来检查一个小时是否介于另外两个特定时间之间:

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。如何解决这个问题?

4

2 回答 2

4

这是一个使用它的解决方案LocalTime,它也可以正确处理午夜之后的当前和关闭时间。

String openHour = "08:00 AM";
String currentHour = "01:00 PM";
String closeHour = "02:00 AM";

DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "hh:mm a" , Locale.US );
LocalTime openTime = LocalTime.parse(openHour, formatter);
LocalTime currentTime  = LocalTime.parse(currentHour, formatter);
LocalTime closeTime = LocalTime.parse(closeHour, formatter);

boolean isOpen = false;
if (closeTime.isAfter(openTime)) {
  if (openTime.isBefore(currentTime) && closeTime.isAfter(currentTime)) {
    isOpen = true;
  }
} else if (currentTime.isAfter(openTime) || currentTime.isBefore(closeTime)) {
  isOpen = true;
}

if (isOpen) {
  System.out.println("We are open");
} else {
  System.out.println("We are closed");
}
于 2019-04-09T19:22:18.270 回答
2

正如迈克尔普拉特建议的那样,只需要滚动一天:

import java.util.*;
import java.text.*;

public class Foo {
  public static void main(String[] args) throws Exception {

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

    if (closeCalendar.before(openCalendar)) {
      closeCalendar.add(Calendar.DAY_OF_YEAR, 1);
    }

    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!");
    }
  }
}
于 2019-04-09T18:53:21.447 回答