sách gpt4 ai đã đi

java - 如何检查两个日期是否在一个时间段(间隔)之间?

In lại 作者:行者123 更新时间:2023-11-29 03:24:07 26 4
mua khóa gpt4 Nike

我的输入是开始日期和结束日期。我想检查它是在 12 月 1 日到 3 月 31 日之间。(年份可以更改,并且只有在此期间内或之外的日期)。

到目前为止,我还没有找到任何关于 Joda-time 的解决方案。有人可以给我一个如何做的起点(不是代码,只是逻辑)?我还没有检查代码,但是它非常丑陋,我想找到一个算法解决方案

public static boolean isInWinter(Contract contract) {
logger.info("begin isInWinter:");
DateTime beginDate = contract.getBeginningDate();
DateTime endDate = contract.getEndDate();
/*
* If the year is different (etc 2012 dec,2013 marc) check that the
* beginning month is december, and the end month is jan,feb,marc
*/
if (endDate.getYear() - beginDate.getYear() == 1) {
if ((beginDate.getMonthOfYear() == 12)
&& ((endDate.getMonthOfYear() == 1
|| endDate.getMonthOfYear() == 2 || endDate
.getMonthOfYear() == 3))) {
logger.info("return true different year");
trả về giá trị đúng;
}
/*
* Same year can be if begin and end date is december or begin and
* and date is jan,febr,marc TODO REMOVE Auto formatter
*/
} else if (endDate.getYear() - beginDate.getYear() == 0) {
if ((beginDate.getMonthOfYear() == 12 && endDate.getMonthOfYear() == 12)
|| ((beginDate.getMonthOfYear() == 1
|| beginDate.getMonthOfYear() == 2 || beginDate
.getMonthOfYear() == 3) && (endDate
.getMonthOfYear() == 1
|| endDate.getMonthOfYear() == 2 || endDate
.getMonthOfYear() == 3))) {
logger.info("return true same year");
trả về giá trị đúng;
}

}
logger.info("return false");
trả về false;
}

1 Câu trả lời

Joda Time

Joda-Time及其 Intervalsử dụng overlap 方法的类正是您所需要的。非常简单。

时区

与 java.util.Date 不同,DateTime 真正知道其分配的时区。通常指定比依赖默认更好。请注意,如果您正在跟踪日期+时间(DateTime 而不是 LocalDate),则时区至关重要。 “一天”开始和结束的定义取决于时区。为此,跨时区工作的企业可能会选择使用 UTC。

时间跨度

仅供引用,除了 Interval 之外,Joda-Time 还提供了用于处理时间跨度的 Period 和 Duration 类。

一天的开始

当处理日期时间值但关注“一天”时,请将时间部分调整为该特定时区当天的第一时刻。在 Joda-Time 中,只需调用 withTimeAtStartOfDay phương pháp.

避免与“午夜”相关的类和方法。 Joda-Time 团队不再推荐它们。

半开

Joda-Time 在比较时间跨度时使用“半开”方法。开始是包容的,结束是排他的。这意味着,由于您希望从 12 月 1 日到 3 月 31 日,我们定义了 the first moment of the day of December 1 ĐẾN the first moment of the day of April 1 .如果您深思熟虑,并在 StackOverflow 上搜索有关“joda interval”的其他帖子,您就会发现这种方法的智慧。

比较

Interval 类提供方便的比较方法:contains ,邻接,chồng chéo间隙。请务必阅读文档以了解具体细节。

java.time

Java 8 带来了新的 java.time 包,它的灵感来自 Joda-Time,由 JSR 310 定义。Joda-Time 继续在 Java 8 中工作,但如果您希望学习 java.time 而不是 Joda-Time是日期时间工作的新手。

示例代码

DateTimeZone timeZone = DateTimeZone.forID( "Europe/Budapest" ); // Or, perhaps DateTimeZone.UTC

// Đầu vào
DateTime contractStart = new DateTime( 2014, 1, 2, 0, 0, 0, timeZone );
DateTime contractStop = new DateTime( 2014, 5, 6, 0, 0, 0, timeZone );
Interval contractInterval = new Interval( contractStart, contractStop );

// Target to test
DateTime targetStart = new DateTime( 2013, DateTimeConstants.DECEMBER, 1, 0, 0, 0, timeZone );
DateTime targetStop = targetStart.plusMonths( 4 );
Interval targetInterval = new Interval( targetStart, targetStop );

boolean targetContainsContract = targetInterval.contains( contractInterval );
boolean targetOverlapsContract = ( targetInterval.overlap( contractInterval ) != null );

转储到控制台...

System.out.println( "contractInterval: " + contractInterval );
System.out.println( "targetInterval: " + targetInterval );
System.out.println( "targetContainsContract: " + targetContainsContract );
System.out.println( "targetOverlapsContract: " + targetOverlapsContract );

运行时...

contractInterval: 2014-01-02T00:00:00.000+01:00/2014-05-06T00:00:00.000+02:00
targetInterval: 2013-12-01T00:00:00.000+01:00/2014-04-01T00:00:00.000+02:00
targetContainsContract: false
targetOverlapsContract: true

关于java - 如何检查两个日期是否在一个时间段(间隔)之间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21973842/

26 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com