简体   繁体   English

我应该使用哪种方法?

[英]Which method should I use?

I'm an intermediate java coder, writing a librarian Java app, and for some reason I just can't figure out whether I should use dueDate.after(today) OR dueDate.before(today) method as to deciding if the book is overdue. 我是一名中级Java程序员,正在编写图书管理员Java应用程序,由于某种原因,我无法确定是否应该使用dueDate.after(today)dueDate.before(today)方法来确定这本书是否逾期。 I've gotten quite some contradictory values by typing both the methods. 通过键入这两种方法,我已经获得了一些相互矛盾的价值观。 Hence, I'm also assuming there is some other bug in my code as well, so it would be nice if you can conform which is the correct method so that I can move on the fixing the other bug. 因此,我还假设我的代码中也存在其他错误,因此,如果您能够遵循正确的方法,那将是很好的,这样我就可以继续解决其他错误。

You need dueDate.before(today) : the due date is before today; 您需要dueDate.before(today) :截止日期在今天之前; the due date is in the past, so the book is past due. 截止日期是过去的日期,所以这本书已经过期了。

Maybe it's easier if you swap the objects around? 交换对象也许更容易? You'd get today.after(dueDate) and if you read that out loud, it suddenly becomes quite clear: "if today is after the due date, then ..." 您会在today.after(dueDate) ,然后大声朗读,这突然变得很清楚:“如果今天在到期日之后,则...”

Remember that the before and after methods perform a < (or > ) comparison, rather than a <= (or >= comparison). 请记住, beforeafter方法执行< (或> )比较,而不是<= (或>=比较)。 That is what is meant by the word "strictly" in the API documentation. 这就是API文档中“严格”一词的含义。

Also, Java Date objects are really an instant in time, rather than what people usually think of as a "date". 而且,Java Date对象实际上是即时的,而不是人们通常认为的“日期”。 That is, it won't just compare the day, but the time of day. 也就是说,它不仅会比较日期,而且会比较时间。

If you want to compare only the day, without checking the time during that day, create all of your due dates to be at a specific time, like midnight. 如果您只想比较一天,而不检查当天的时间,则将所有到期日期创建为特定时间,例如午夜。 For example, suppose that a book is due October 26. The due date could be midnight, October 27. 例如,假设一本书应于10月26日到期。截止日期可以是10月27日午夜。

boolean overdue = !now.before(dueDate);

The somewhat awkward negation accounts for the case when it is now exactly 12:00 AM Oct. 27. 有点尴尬的否定说明了现在是10月27日上午12:00的情况。

dueDate.before(today) would translate to the actual dueDate of the book occurring before today, meaning that it is actually overdue. dueDate.before(today)将转换为该书在今天之前的实际dueDate.before(today) ,这意味着该书实际上已过期。 This is probably what you want (assuming you are checking for true) 这可能就是您想要的(假设您要检查的是真的)

the other side of things 事物的另一面

dueDate.after(today) would translate to the actual dueDate of the book occurring after today, meaning that it is not yet over due. dueDate.after(today)会转换为该书在今天之后发生的实际DueDate,表示该书尚未到期。

The only difference between !dueDate.after(today) and dueDate.before(today) is the result when both dates are exactly the same - presumably a book is not overdue when returned on the due date, so dueDate.before(today) should be correct. !dueDate.after(today)dueDate.before(today)之间的唯一区别是两个日期完全相同时的结果-假定在到期日归还书还没有过期,所以dueDate.before(today)应该是正确的。

As for your unspecified other problems: are you aware that java.util.Date represents a millisecond-precision point in time, not a calendar date? 至于您未指定的其他问题:您是否知道java.util.Date代表毫秒精度的时间点,而不是日历日期? That means that in order to use its comparison methods for calendar dates, you have to make very sure you set the time components to zero when creating your Date instances. 这意味着,为了将其比较方法用于日历日期,必须确保在创建Date实例时将时间分量设置为零。 Another cause of problems could be time zone differences. 造成问题的另一个原因可能是时区差异。

It depends on what you want to achieve. 这取决于您要实现的目标。 Is dueDate the date you want to set when lending a book? 借书时,dateDate是要设置的日期吗? The due date is derived from the lending date of the book, so I would try an approach like book.isDue(today) assuming that the book object contains the lending and due dates as attributes. 截止日期是从书的借出日期中得出的,因此,我将假设book对象包含借出和截止日期作为属性,尝试使用诸如book.isDue(today)这样的方法。

With questions like this it helps if you make it explicit for yourself which objects are involved and what their relations are before you design the actions between the objects. 对于类似这样的问题,如果在设计对象之间的动作之前先明确指出涉及哪些对象以及它们之间的关系,将很有帮助。

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

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