比较日期和时间范围的 Java 代码部分:

huangapple go评论62阅读模式
英文:

Compare Date and Time range with Java

问题

我有这个输入:

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

这是一个需要与以下内容进行比较的日期和时间:

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

最佳的比较方法是什么,以确认availability实际上包含了一个包含查询中时间02:39:00的日期。

因此,它可以用Java代码表示为:

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

在这种情况下,isOpen的值将为true。

英文:

I have this input:

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

Which is a DAY and time that needs to compared to

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

As such, it can be represented in Java-form:

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

And which in this case, the value of isOpen would be true.

答案1

得分: 1

我会通过使用两个以日期为键、起始时间为第一个映射的值,结束时间为第二个映射的值的映射来解决这个问题。

然后检查时间是否在这个时间范围内。

您可以使用gson将JSON转换为Java对象。

英文:

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.

答案2

得分: 1

正如Pranav balu所说,使用Java对象/数据结构来存储您的数据。使用像Jackson或Gson这样的JSON库将您的JSON输入转换为Java类型。您将需要一个Java类型来表示每日的可用时间范围。例如:

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

我提供了一个接受String参数的便捷构造函数和便捷方法。您可能希望有一个接受LocalTime的构造函数和一个接受两者都能处理的方法。

示例用法:

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

输出:

> Is open? true

我利用了您的时间字符串符合ISO 8601格式的事实,而LocalTime会将此格式作为其默认格式进行解析,也就是说,不需要任何显式的格式化器。格式中的秒是可选的,因此00:0002:39:00都可以解析。

英文:

As Pranav balu already said, use Java objects/data structures for your data. Use a JSON library like Jackson or Gson for converting your JSON input to Java types. You will need a Java type for the daily availability range. 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)) &amp;&amp; time.isBefore(closes);
	}
}

I have provided a convenience constructor and a convenience method that accept String arguments. You may want a constructor and a method that accept LocalTime, or both.

Example use:

		Map&lt;DayOfWeek, AvailabilityRange&gt; availability
				= new EnumMap&lt;DayOfWeek, AvailabilityRange&gt;(
						Map.of(DayOfWeek.SUNDAY, new AvailabilityRange(&quot;00:00&quot;, &quot;10:00&quot;), 
								DayOfWeek.MONDAY, new AvailabilityRange(&quot;00:00&quot;, &quot;10:00&quot;)));
		
		String dayString = &quot;MONDAY&quot;;
		String timeString = &quot;02:39:00&quot;;
		
		boolean isOpen;
		AvailabilityRange availabilityForDay
				= availability.get(DayOfWeek.valueOf(dayString));
		if (availabilityForDay == null) {
			isOpen = false;
		} else {
			isOpen = availabilityForDay.inRange(timeString);
		}
		
		System.out.println(&quot;Is open? &quot; + 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. The seconds are optional in the format, so both 00:00 and 02:39:00 are parsed.

huangapple
  • 本文由 发表于 2020年10月17日 02:45:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64394776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定