简体   繁体   English

使用 Java 比较日期和时间范围

[英]Compare Date and Time range with Java

I have this input:我有这个输入:

{
  "MONDAY" : "02:39:00"
}

Which is a DAY and time that needs to compared to这是需要比较的 DAY 和时间

  "availability" : {
    "SUNDAY" : {
      "from" : "00:00",
      "to" : "10:00"
    },
    "MONDAY" : {
      "from" : "00:00",
      "to" : "10:00"
    }
  }

What's the best way to compare if the availability in fact contains a DAY in which the time in the query 02:39:00 is within.比较availability实际上是否包含查询02:39:00的时间在其中的 DAY 的最佳方法是什么。

As such, it can be represented in Java-form:因此,它可以用 Java 形式表示:

boolean isOpen = checkIfOpen("MONDAY", "02:39:00", availabilityJSONObject);

And which in this case, the value of isOpen would be true.在这种情况下, isOpen的值为 true。

I would approach this problem by using two maps with key as day and from time as value for the first map and to time as value for the second map.我将通过使用两个地图来解决这个问题,其中键为日期,时间为第一张地图的值,时间为第二张地图的值。

And check if the time falls between this time.并检查时间是否介于此时间之间。

You could use gson to convert the json to java objects.您可以使用 gson 将 json 转换为 java 对象。

As Pranav balu already said, use Java objects/data structures for your data.正如 Pranav balu 已经说过的那样,为您的数据使用 Java 对象/数据结构。 Use a JSON library like Jackson or Gson for converting your JSON input to Java types.使用 Jackson 或 Gson 等 JSON 库将您的 JSON 输入转换为 Java 类型。 You will need a Java type for the daily availability range.对于每日可用性范围,您将需要一个 Java 类型。 For example:例如:

public class AvailabilityRange {

    LocalTime opens;
    LocalTime closes;
    
    public AvailabilityRange(String from, String to) {
        opens = LocalTime.parse(from);
        closes = LocalTime.parse(to);
    }
    
    public boolean inRange(String timeString) {
        LocalTime time = LocalTime.parse(timeString);
        return (! time.isBefore(opens)) && time.isBefore(closes);
    }
}

I have provided a convenience constructor and a convenience method that accept String arguments.我提供了一个方便的构造函数和一个接受String参数的方便的方法。 You may want a constructor and a method that accept LocalTime , or both.您可能需要一个构造函数和一个接受LocalTime的方法,或两者​​兼而有之。

Example use:使用示例:

        Map<DayOfWeek, AvailabilityRange> availability
                = new EnumMap<DayOfWeek, AvailabilityRange>(
                        Map.of(DayOfWeek.SUNDAY, new AvailabilityRange("00:00", "10:00"), 
                                DayOfWeek.MONDAY, new AvailabilityRange("00:00", "10:00")));
        
        String dayString = "MONDAY";
        String timeString = "02:39:00";
        
        boolean isOpen;
        AvailabilityRange availabilityForDay
                = availability.get(DayOfWeek.valueOf(dayString));
        if (availabilityForDay == null) {
            isOpen = false;
        } else {
            isOpen = availabilityForDay.inRange(timeString);
        }
        
        System.out.println("Is open? " + isOpen);

Output:输出:

Is open?开了? true真的

I am exploiting the fact that your time strings are in ISO 8601 format, and LocalTime parses this format as its default, that is, without any explicit formatter.我正在利用您的时间字符串采用 ISO 8601 格式这一事实, LocalTime将此格式解析为默认格式,即没有任何显式格式化程序。 The seconds are optional in the format, so both 00:00 and 02:39:00 are parsed.秒在格式中是可选的,因此00:0002:39:00都被解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM