gpt4 book ai didi

java - 日期范围内的日期范围

转载 作者:行者123 更新时间:2023-11-30 07:12:01 25 4
gpt4 key购买 nike

实际上这个任务对我来说似乎很容易,但我有点卡住了,感谢您的一些提示 :D

我有一些事件有开始时间和结束时间 - 我想创建一个包含日历周的表格。

因此我写了一个方法来检查事件是否在本周内并像这样给它着色:

enter image description here

private boolean inWeek(Date date, Entry pe) {
return ((pe.getStartsAt().after(Util.firstDayOfWeek(date)) || pe.getStartsAt().equals(Util.firstDayOfWeek(date)))
&& (pe.getEndsAt().before(Util.lastDayOfWeek(date)) || pe.getEndsAt().equals(Util.lastDayOfWeek(date))));
}

如果事件只持续一周,这种情况还可以。但是,如果 Activity 在本周之前开始,或在本周之后结束,甚至持续数周怎么办?

它变得非常复杂,我目前的解决方案是这样的:

private boolean inWeek(Date date, Entry pe) {

return ( pe.getStartsAt().after(Util.firstDayOfWeek(date)) && pe.getEndsAt().after(Util.firstDayOfWeek(date)) && pe.getEndsAt().before(Util.lastDayOfWeek(date)) )
|| ( pe.getStartsAt().before(Util.lastDayOfWeek(date)) && pe.getStartsAt().after(Util.firstDayOfWeek(date)) && pe.getEndsAt().after(Util.lastDayOfWeek(date)) )
|| ( pe.getStartsAt().after(Util.firstDayOfWeek(date)) && pe.getEndsAt().before(Util.lastDayOfWeek(date)) )
|| ( pe.getStartsAt().before(Util.firstDayOfWeek(date)) && pe.getEndsAt().after(Util.lastDayOfWeek(date)) );

}

但在某些单元格中仍然没有显示正确的颜色。有人对我有什么提示吗?

(...没有提出 joda 时间 ^^)

最佳答案

我花了相当多的时间来处理……好吧,时间……我可以告诉你,我更愿意让别人为我完成这项工作。

为此,如果你愿意试一试,我会看看 JodaTime

基本上,这个例子的作用是创建一系列 Interval。一个是“期间”,或一年中的一周(从星期一开始到星期日结束)。

一个Interval是重叠区间,跨越“period”前后一周,另一个是“period”内的单日Interval

import org.joda.time.Interval;
import org.joda.time.MutableDateTime;
import org.joda.time.Period;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;
import org.joda.time.format.DateTimeParser;

public class TestTimeInterval {

public static void main(String[] args) {

DateTimeFormatter formatter = new DateTimeFormatterBuilder().
appendDayOfMonth(2).appendLiteral(" ").
appendDayOfWeekText().appendLiteral(" ").
appendMonthOfYearText().appendLiteral(" ").
appendYear(2, 2).
toFormatter();

// Last week
MutableDateTime targetStart = MutableDateTime.now();
targetStart.setDayOfWeek(1);
targetStart.addDays(-6);

// Next week
MutableDateTime targetEnd = MutableDateTime.now();
targetEnd.setDayOfWeek(7);
targetEnd.addDays(7);

System.out.println("Target range = " + formatter.print(targetStart) + " to " + formatter.print(targetEnd));
Interval targetInterval = new Interval(targetStart, targetEnd);

// This week
MutableDateTime start = MutableDateTime.now();
start.setDayOfWeek(1);

MutableDateTime end = MutableDateTime.now();
end.setDayOfWeek(7);

Interval interval = new Interval(start, end);

System.out.println("Interval range = " + formatter.print(start) + " to " + formatter.print(end));
System.out.println("Contains interval = " + targetInterval.contains(interval));

// Last week
targetStart = DateTime.now();

// Next week
targetEnd = DateTime.now();

System.out.println("Target range = " + formatter.print(targetStart) + " to " + formatter.print(targetEnd));
targetInterval = new Interval(targetStart, targetEnd);
System.out.println("Contains interval = " + interval.contains(targetInterval));
}

}

哪些输出...

Target range = 10 Tuesday December 2013 to 29 Sunday December 2013
Period range = 16 Monday December 2013 to 22 Sunday December 2013
Contains period = true
Target range = 19 Thursday December 2013 to 19 Thursday December 2013
Contains period = true

你最终得到的结果只需要通过两种方式检查周期间隔。

  1. 检查“期间”是否在提供的Interval
  2. 如果提供的 Interval 在“周期”内...

例如……

 if (period.contains(interval) || interval.contains(period)) {
// Match...
}

现在,还有很多其他事情需要考虑,例如,如果时间对 Interval 不重要,您需要将时间归零(周期的开始应该是午夜/早上,结束应该是午夜晚上)所以你可以最大化捕获区域

更新以更好地利用 JodaTime 库

@BasilBourque 能够突出显示原始示例的一些问题,我已经相应地对其进行了更新和测试。谢谢@BasilBourque

虽然与原始版本相似,但它更好地使用了 JodaTime

public static void newWay() {

DateTimeFormatter formatter = new DateTimeFormatterBuilder().
appendDayOfMonth(2).appendLiteral(" ").
appendDayOfWeekText().appendLiteral(" ").
appendMonthOfYearText().appendLiteral(" ").
appendYear(2, 2).
toFormatter();

// Last week
DateTime targetStart = DateTime.now(DateTimeZone.getDefault()).
withDayOfWeek(DateTimeConstants.MONDAY).
minusDays(6);
//MutableDateTime targetStart = MutableDateTime.now();
//targetStart.setDayOfWeek(1);
//targetStart.addDays(-6);

// Next week
DateTime targetEnd = DateTime.now(DateTimeZone.getDefault()).
withDayOfWeek(DateTimeConstants.SUNDAY).
plusDays(7);
//MutableDateTime targetEnd = MutableDateTime.now();
//targetEnd.setDayOfWeek(7);
//targetEnd.addDays(7);

System.out.println("Target range = " + formatter.print(targetStart) + " to " + formatter.print(targetEnd));
Interval targetInterval = new Interval(targetStart, targetEnd);

// This week
DateTime start = DateTime.now(DateTimeZone.getDefault()).
withDayOfWeek(DateTimeConstants.MONDAY);
//MutableDateTime start = MutableDateTime.now();
//start.setDayOfWeek(1);

DateTime end = DateTime.now(DateTimeZone.getDefault()).
withDayOfWeek(DateTimeConstants.SUNDAY);
//MutableDateTime end = MutableDateTime.now();
//end.setDayOfWeek(7);

Interval interval = new Interval(start, end);

System.out.println("Period range = " + formatter.print(start) + " to " + formatter.print(end));

System.out.println("Contains period = " + targetInterval.contains(interval));

// Last week
targetStart = DateTime.now();

// Next week
targetEnd = DateTime.now();

System.out.println("Target range = " + formatter.print(targetStart) + " to " + formatter.print(targetEnd));
targetInterval = new Interval(targetStart, targetEnd);
System.out.println("Contains period = " + interval.contains(targetInterval));

}

关于java - 日期范围内的日期范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20677541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com