Java – 如何检查一个 LocalTime 对象是否在午夜之前

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

Java - How to check is a LocalTime object is before midnight

问题

你的Java代码中有一个问题,即解析时间字符串时,你使用了HTML编码的引号“"”而不是正常的双引号。这可能导致解析错误。请修改代码如下:

public boolean checkTime() {
        
        final LocalTime startTime = LocalTime.parse("23:00");
        final LocalTime endTime = LocalTime.parse("07:00");

        return startTime.isBefore(LocalTime.MIDNIGHT) // true
                && endTime.isAfter(LocalTime.MIDNIGHT); // true
}

这个修改后的代码应该会正确地返回true,因为23:00确实在午夜之前,而07:00在午夜之后,满足了你的条件。

英文:

I am trying to determine if a particular LocalTime is before Midnight. Background basically I want to know if a shift's start/end time bleed over into the next day. For example if a shift was scheduled for 23:00 - 07:00, then this would be considered bleeding into the next day since the end time is after midnight and the start time is before.

I began to implement this by seeing if the start time is before midnight, and seeing if the end time is after midnight. This would meet the criteria of being overlapping into the next day.

In Java my code looks like this

public boolean checkTime() {
        
        final LocalTime startTime = LocalTime.parse("23:00");
        final LocalTime endTime = LocalTime.parse("07:00");

        return startTime.isBefore(LocalTime.MIDNIGHT) //false
                && endTime.isAfter(LocalTime.MIDNIGHT)) //true;
}

Why does it return false for the start time being not before midnight? Isn't the time of 23:00 one hour before midnight? I would expect this to return true. Maybe I am misunderstanding something? I am working with LocalTimes and not using LocalDateTimes

答案1

得分: 6

根据文档,对于isBeforeisAfter两者都是这样的:

比较是基于时间在一天内的时间线位置。

这些方法假定你要比较的时间在同一天,所以你不能用它们来检查时间段是否“延伸到”第二天。

假设一个班次必须少于24小时,你可以通过检查结束时间是否在开始时间之前来检查它是否“延伸到”第二天。这利用了isBefore的这一特性,即“假设两个时间在同一天”。如果isBefore认为endTimestartTime之前,那就意味着endTime在第二天。

endTime.isBefore(startTime)
英文:

According to the documentation, for both isBefore and isAfter:

> The comparison is based on the time-line position of the time within a day.

These methods assume that the times you are comparing are on the same day, so you cannot use these to check if the period "bleeds into" the second day.

Assuming a shift must be less than 24 hours, you can check if it "bleeds into" the second day by checking if the end is before the start. This takes advantage of this "assuming the two times are in the same day" characteristic of isBefore. If isBefore thinks that endTime is before startTime, that must mean endTime is in the next day.

endTime.isBefore(startTime)

答案2

得分: 3

以下是翻译好的部分:

与所有事物一样,如果有疑虑,请查看文档。

以下是有关您的代码各部分的文档。

重要的是 LocalTime::isBefore 方法。看看它说了什么。

检查此时间是否在指定时间之前。比较基于时间线上的时间位置 在一天内

那个加粗的部分可能不是很清楚,但意思是它将会询问 23 是否在 同一天0 之前。

更具体地说,是否在 5月18日 23:00 之前是5月18日 00:00。正如您所看到的,答案是否定的,因为您不小心要求在同一天。

您需要使用不同的工具,可能是 LocalDateTime,才能解决这个问题。如果您在使用该工具时遇到问题,请告诉我,我可以发布一个示例。

英文:

As with all things, when in doubt, check the documentation.

Here is the documentation for the various parts of your code.

The important one is the LocalTime::isBefore method. Look at what it says.

Checks if this time is before the specified time. The comparison is based on the time-line position of the time within a day.

That bolded part isn't completely clear, but what that means is that, it is going to ask if 23 is before 0 on the same day.

More specifically, if May 18 23:00 is before May 18 00:00. As you can see, the answer is false because you accidentally asked for the same day.

You need to use a different tool, likely LocalDateTime, to be able to solve this problem. Let me know if you have trouble using that tool instead and I can post an example.

答案3

得分: 3

LocalTime 只知道时间,不知道日期。LocalTime.MIDNIGHT 表示的是一天开始的午夜时间,'00:00'。这就是你的代码工作方式的原因。

因为23:0000:00之后,所以

startTime.isBefore(LocalTime.MIDNIGHT) == false

在这种情况下,

endTime.isAfter(LocalTime.MIDNIGHT)) == true

因为7:0000:00之后。

你应该考虑使用LocalDateTime

英文:

LocalTime only knows the time. It doesn't know anything about the date. LocalTime.MIDNIGHT is The time of midnight at the start of the day, '00:00'.. That's the reason why your code is working like that.

startTime.isBefore(LocalTime.MIDNIGHT) == false

because 23:00 is after 00:00.

In the case of

endTime.isAfter(LocalTime.MIDNIGHT)) == true

because 7:00 is after 00:00

You should consider to use LocalDateTime

huangapple
  • 本文由 发表于 2023年5月18日 12:23:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277716.html
匿名

发表评论

匿名网友

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

确定