[英]Which is the best way to represent the business opening hours in a Java object?
我需要表示开放时间,以及在特定日期和时间返回真/假的方法。 有没有已经有这个功能的包?
编辑:基本上我需要用数据库或文件中的数据构造一个对象,然后对该对象执行基本检查,比如它是否在某个时刻关闭。
问题是一些企业的工作时间在 00:00 之后,所以它会在第二天重叠。 在这种情况下,我发现该对象应该能够每天支持多个时间范围,也可以用于午餐刹车。
您可以使用Calendar
创建课程,并查看其工作日和工作时间。
class BusinessHour{
public void isOpenNow(){
Calendar calNow = Calendar.getInstance();
//check the rules for example , if day is MOn-FRi and time is 9-18.
}
}
我不认为会有一个现成的,因为每个企业都有自己的规范,如果可以配置以便它可以修复所有业务,可以在类外部提供conf参数以便更好地设计
我使用以下Object在HashSet中存储OpeningHours。 使用Midnight而不是“800”或“1600”的小时数可以很容易地简化下面的示例,但我喜欢这种方式。
public class OpeningHours {
public enum DAY {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public OpeningHours(DAY day, Integer from, Integer to) {
this.day = day;
this.from = from; // format time using 800 for 8:00am or 2300 for 23:00
this.to = to;
}
@Override
public String toString() {
return "OpeningHours [day=" + day + ", from=" + from + ", to=" + to + ", isAllDay=" + isAllDay + "]";
}
public OpeningHours() {
}
public DAY day;
public Integer from;
public Integer to;
public boolean isAllDay = false;
public void isOpenx(DateTime start) {
}
public boolean isOpen(DateTime start) {
if (day.ordinal() != start.getDayOfWeek() - 1) {
return false;
}
if (isAllDay)
return true;
String f = String.format("%04d", from);
String t = String.format("%04d", to);
Integer fh = Integer.valueOf(f.substring(0, 2));
Integer fm = Integer.valueOf(f.substring(2));
Integer th = Integer.valueOf(t.substring(0, 2));
Integer tm = Integer.valueOf(t.substring(2));
DateTime intStart = start.withHourOfDay(fh).withMinuteOfHour(fm);
DateTime intEnd = start.withHourOfDay(th).withMinuteOfHour(tm);
if (intStart.equals(start) || intEnd.equals(start)) {
return true;
}
if (intStart.isBefore(start) && intEnd.isAfter(start)) {
return true;
}
return false;
}
}
HashSet<OpeningHours> hours = new HashSet<OpeningHours>();
hours.add(new OpeningHours(OpeningHours.DAY.MONDAY, 800, 1200));
hours.add(new OpeningHours(OpeningHours.DAY.MONDAY, 1230, 1600));
DateTime dateToCheck = new DateTime(2012, 9, 4, 8, 00, 0, 0);
for (OpeningHours item : hours) {
boolean isOpen = item.isOpen(dateToCheck );
if (isOpen){
System.out.println("Is Open!");
}
}
在Java 8中,您可以将其表示为一组openingTimes对象:
套装:
Set<OpeningTimes> openingTimes = new HashSet<>();
开放时间
import java.time.DayOfWeek;
import java.time.LocalTime;
public class OpeningTimes {
private DayOfWeek dayOfWeek;
private LocalTime from;
private LocalTime to;
public DayOfWeek getDayOfWeek() {
return dayOfWeek;
}
public void setDayOfWeek(DayOfWeek dayOfWeek) {
this.dayOfWeek = dayOfWeek;
}
public LocalTime getFrom() {
return from;
}
public void setFrom(LocalTime from) {
this.from = from;
}
public LocalTime getTo() {
return to;
}
public void setTo(LocalTime to) {
this.to = to;
}
}
可能没有一个包已经实现了您的确切业务逻辑。
例如,您是否需要覆盖一年中特定日期的标准开放日期,例如特殊事件的关闭? 公共假期怎么样?你打开这些吗?
我简单的建议解决方案是:
下面的类还考虑了某物(例如建筑物)在指定日期根本不开放的情况。
import org.jetbrains.annotations.NotNull;
import java.time.LocalTime;
import java.util.Objects;
/**
* {@code OpeningHours} represents the opening hours of a single day of a "thing"
* (e.g. a building). The opening- and closing hour are independent of timezones.
*/
public class OpeningHours {
private boolean opensToday;
private LocalTime openingHour;
private LocalTime closingHour;
/**
* A reusable {@code OpeningHours} that is always closed.
*/
private static OpeningHours closedInstance = new OpeningHours();
/**
* Constructs a new {@code OpeningHours} for a thing that does not open at this specific day.
*/
public OpeningHours() {
opensToday = false;
}
/**
* Constructs a new {@code OpeningHours} for a thing that does open today.
*
* @param openingHour the opening hour of the thing, inclusive. Must be strictly less than the closingHour
* @param closingHour the closing hour of the thing, exclusive. Must be strictly more than the openingHour
*/
public OpeningHours(@NotNull LocalTime openingHour, @NotNull LocalTime closingHour) {
Objects.requireNonNull(openingHour);
Objects.requireNonNull(closingHour);
if (openingHour.compareTo(closingHour) >= 0)
throw new IllegalArgumentException("the openingHour must be strictly less than the closingHour");
this.opensToday = true;
this.openingHour = openingHour;
this.closingHour = closingHour;
}
/**
* Returns whether the thing this {@code OpeningHours} belongs to will open today.
*
* @return the value of {@link #opensToday}
*/
public boolean opensToday() {
return opensToday;
}
/**
* Returns whether the provided {@code time} is within the opening hours. More specifically this method returns
* {@code true} when {@code time} is equal to or greater than {@code this#getOpeningHour()} and strictly less
* than {@code this.getClosingHour()}.
*
* @param time the time at wh
* @return {@code true} if {@code time} is greater than or equal to {@link #openingHour} and strictly less than
* {@link #closingHour}.
*/
public boolean isOpen(LocalTime time) {
if (!opensToday)
return false;
return (openingHour.compareTo(time) <= 0) && (closingHour.compareTo(time) > 0) ;
}
/**
* Returns whether the current time is within the opening hours.
*
* @see #isOpen(LocalTime)
*/
public boolean isOpen() {
return this.isOpen(LocalTime.now());
}
/**
* @return an {@code OpeningHours} for a thing that is permanently closed on this day.
*/
public OpeningHours getClosedInstance() {
return closedInstance;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
OpeningHours that = (OpeningHours) o;
if (opensToday != that.opensToday) return false;
if (!Objects.equals(openingHour, that.openingHour)) return false;
return Objects.equals(closingHour, that.closingHour);
}
@Override
public int hashCode() {
int result = (opensToday ? 1 : 0);
result = 31 * result + (openingHour != null ? openingHour.hashCode() : 0);
result = 31 * result + (closingHour != null ? closingHour.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "OpeningHours{" +
"opensToday=" + opensToday +
", openingHour=" + openingHour +
", closingHour=" + closingHour +
'}';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.