实际上这个任务对我来说似乎很容易,但我有点陷入困境,并会感谢一些提示:D
我有一些有开始和结束时间的事件 - 我想创建一个包含日历周的表格。
因此,我写了一个方法来检查一个事件是否在本周内为它着色:
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))));
}
如果事件持续一周,这个案子还可以。但如果事件在本周之前开始,或在本周之后结束甚至持续数周,该怎么办?
它变得非常复杂,我目前的解决方案是:
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时间^^)
答案 0 :(得分:2)
花了我相当多的时间来捣乱...好吧,时间......我可以告诉你,我宁愿让其他人为我做这项工作。
为此,如果你愿意试一试,我会看看JodaTime
基本上,这个例子创建了一系列Interval
。一个是“期间”,或一年中的一周(从星期一开始到星期日结束)。
一个Interval
是重叠间隔,跨越“期间”之前一周和一周之后,另一个是“期间”内的单日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
您最终只需要以两种方式检查周期间隔。
Interval
和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));
}
答案 1 :(得分:2)
Joda-Time 2.3库使这项工作变得更加容易。它包含Interval类和overlap
方法。
有关类似的代码和讨论,请参阅the answer by MadProgrammer。
Interval类很聪明,考虑到包含间隔的开始和结束排除。您应该根据等于或大于开头的逻辑进行比较,但不要超过停止。为什么?因为新的一天之前的那一刻是无限可分的。您可能会想,“好吧java.util.Date& Joda-Time解析为毫秒,所以我将使用.999”。但是当你将代码移植到Java 8的新java.time。*类时,你会感到惊讶,其中时间可以解析为纳秒。
要支持此比较,请注意通过调用withTimeAtStartOfDay
来定义目标周。使用此方法而不是尝试通过设置零时间元素来创建午夜。此方法很智能,可处理夏令时和其他异常情况,在某些时区某些天可能没有00:00:00午夜时间。
指定时区而不是依赖默认值。其他答案中的所有代码都无法解决时区问题。这意味着他们使用JVM的默认时区。因此,当部署到设置为其他时区的其他计算机时,此应用程序会得到不同的结果。使用proper time zone names,绝不使用3个字母的代码。
如果您的应用适用于跨时区的人员和地点,则应考虑将目标周基于UTC / GMT(无时区偏移)。请注意,这是StackOverflow每天跟踪您的活动所做的事情。 “天”由UTC / GMT定义。
这些观点证明了为什么你不应该推出自己的日期时间逻辑。请使用称职的图书馆。在Java中,这意味着{8}中的Joda-Time或新的java.time。*类(受Joda-Time启发)。
顺便说一下,ISO 8601 standard defines a "week"正是如此。各国越来越多的公司和行业正在采用这一标准。遵循该标准可能证明是有用的。 Joda-Time DateTime实例知道其ISO周编号。致电myDateTime.weekOfWeakYear().get()
。
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Berlin" );
Interval weekInQuestion = new Interval( new DateTime( 2014, 1, 20, 3, 4, 5, timeZone ).withTimeAtStartOfDay(), new DateTime( 2014, 1, 27, 3, 4, 5, timeZone ).withTimeAtStartOfDay() );
Interval i1 = new Interval( new DateTime( 2014, 1, 2, 3, 4, 5, timeZone ), new DateTime( 2014, 1, 3, 23, 4, 5, timeZone ) );
Interval i2 = new Interval( new DateTime( 2014, 1, 24, 3, 4, 5, timeZone ), new DateTime( 2014, 1, 26, 23, 59, 59, timeZone ) );
Interval i3 = new Interval( new DateTime( 2014, 1, 6, 3, 4, 5, timeZone ), new DateTime( 2014, 1, 30, 3, 4, 5, timeZone ) );
boolean i1HitsWeekInQuestion = i1.overlaps( weekInQuestion );
boolean i2HitsWeekInQuestion = i2.overlaps( weekInQuestion );
boolean i3HitsWeekInQuestion = i3.overlaps( weekInQuestion );
转储到控制台...
System.out.println( "weekInQuestion: " + weekInQuestion );
System.out.println( "i1: " + i1 + " hits week: " + i1HitsWeekInQuestion );
System.out.println( "i2: " + i2 + " hits week: " + i2HitsWeekInQuestion );
System.out.println( "i3: " + i3 + " hits week: " + i3HitsWeekInQuestion );
跑步时......
weekInQuestion: 2014-01-20T00:00:00.000+01:00/2014-01-27T00:00:00.000+01:00
i1: 2014-01-02T03:04:05.000+01:00/2014-01-03T23:04:05.000+01:00 hits week: false
i2: 2014-01-24T03:04:05.000+01:00/2014-01-26T23:59:59.000+01:00 hits week: true
i3: 2014-01-06T03:04:05.000+01:00/2014-01-30T03:04:05.000+01:00 hits week: true
答案 2 :(得分:0)
我尝试了另一种解决方案 - 似乎工作正常,但我不太确定
private boolean inWeek(Date date, Entry pe) {
return ((pe.getEndsAt().after(Util.firstDayOfWeek(date)) || pe.getEndsAt().equals(Util.firstDayOfWeek(date)) )
&& (pe.getStartsAt().before(Util.lastDayOfWeek(date)) || pe.getStartsAt().equals(Util.lastDayOfWeek(date)) ));
}
答案 3 :(得分:0)
我会让代码更易于阅读
private boolean inWeek(Date date, Entry pe) {
Date eventStart = pe.getStartsAt();
Date eventEnd = pe.getEndsAt();
Date firstDay = Util.firstDayOfWeek(date);
Date lastDay = Util.lastDayOfWeek(date);
boolean isInweek = (eventStart.after(firstDay) &&
eventEnd.after(firstDay) &&
eventEnd.before(lastDay))
||
(eventStart.before(lastDay) &&
eventStart.after(firstDay) &&
eventEnd.after(lastDay))
||
(eventStart.after(firstDay) &&
eventEnd.before(lastDay))
||
(eventStart.before(firstDay) &&
eventEnd.after(lastDay));
return isInweek;
}
这可能会使问题更加明显。
您在第二个区块中有 eventEnd.after(lastDay)。我认为没错。
第一个块是多余的,因为第三个块将完全相同。
这不够吗?
private boolean inWeek(Date date, Entry pe) {
Date eventStart = pe.getStartsAt();
Date eventEnd = pe.getEndsAt();
Date firstDay = Util.firstDayOfWeek(date);
Date lastDay = Util.lastDayOfWeek(date);
boolean isInweek =
(eventStart.after(firstDay) && //event is INSIDE of the week.
eventEnd.before(lastDay))
||
(eventStart.before(firstDay) && //event is OUTSIDE of the week
eventEnd.after(lastDay));
return isInweek;
}