如何减去 2 个 java.sql.Date 日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6963111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:02:31  来源:igfitidea点击:

How to subtract 2 java.sql.Date dates?

javadatedate-arithmetic

提问by Иван Бишевац

I insert some data into Sqlite database and check before insert if record for this day allready exists. To check this I have to see if number of days are >0. For example difference (in days) between 2011-8-6 and 2011-8-5 is 1. How to do this in Java?

我将一些数据插入到 Sqlite 数据库中,并在插入之前检查这一天的记录是否已经存在。要检查这一点,我必须查看天数是否 > 0。例如,2011-8-6 和 2011-8-5 之间的差异(以天为单位)是 1。如何在 Java 中做到这一点?

EDIT: As @RayToal mentioned this could be done in database, so I did on that way:

编辑:正如@RayToal 提到的,这可以在数据库中完成,所以我这样做了:

SELECT julianday('now') - julianday(Date(Date)) from VIDEO_HISTORY;

Only problem with this is that it gives me decimal number. For example: 3.3442346529103816 Now I have to decide inside Java if given number is 3 of 4 days. It code is for app that searches youtube for some term and writes statistical data about daily views into database. User is able to schedule job for example every day in 20:00 o'clock. But then he could decide to reschedule this job in 10:00 o'clock, so program has to understood this like difference is one day. So it's obvious that I have to round to first bigger number. Is there some method for this or I have to write myself?

唯一的问题是它给了我十进制数。例如: 3.3442346529103816 现在我必须在 Java 内部决定是否给定的数字是 4 天中的 3 天。它的代码是用于在 youtube 上搜索某个术语并将有关每日观看次数的统计数据写入数据库的应用程序。用户可以安排工作,例如每天 20:00 点。但随后他可以决定在 10:00 点重新安排这项工作,因此程序必须理解这一点,就像差异是一天一样。所以很明显我必须四舍五入到第一个更大的数字。有什么方法可以解决这个问题还是我必须自己写?

EDIT2: According to links provided by @Michael-O this is best solution (using JodaTime):

EDIT2:根据@Michael-O 提供的链接,这是最佳解决方案(使用 JodaTime):

DateTime start = new DateTime(new GregorianCalendar(2011, 8, 4).getTime());
DateTime end = new DateTime(new GregorianCalendar(2011, 8, 8).getTime());

int numberOfDays = Days.daysBetween(start.toDateMidnight(), end.toDateMidnight()).getDays();
System.out.println(numberOfDays);

采纳答案by Michael-O

You may want to consult thistutorial. Moreover you should consider using Joda Time's Periodand consult thisanswer on stackoverflow.

您可能需要查阅教程。此外,您应该考虑使用 Joda TimePeriod并在 stackoverflow 上查阅答案。

回答by JosiahYoder-deactive except..

If you want the number of calendar days(a Periodin java.time parlance), then use (in Java 8):

如果您想要日历天数(java.time 中的点),请使用(在 Java 8 中):

import java.time.temporal.ChronoUnit;
import java.time.LocalDate;
ChronoUnit.DAYS.between(LocalDate.parse(from.toString()),LocalDate.parse(to.toString())

(This avoids a subtle bugin Java 8's java.sql.Date API)

(这避免了Java 8 的 java.sql.Date API 中的一个微妙的错误

If you want a Durationin days (physically elapsed time in 24-hour units), then use Marcelo'sapproach.

如果您想要以天为单位的持续时间(以 24 小时为单位的实际经过时间),请使用Marcelo 的方法。