java – 日期范围中的日期范围

实际上这个任务对我来说似乎很容易,但我有点卡住了,并会感谢一些提示: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时间^^)

解决方法:

花了我相当多的时间来捣乱…好吧,时间……我可以告诉你,我宁愿让其他人为我做这项工作.

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

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

一个间隔是一个重叠间隔,跨越“周期”前一周和一周后,另一个是“周期”内的单日间隔

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在“期间”内……

例如…

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

现在,还有很多其他的事情要考虑,比如,如果时间间隔不重要,你会想要将时间归零(开始时间应该是午夜/早上,结束时间应该是午夜)所以你最大限度地捕获区域

更新了更好地使用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));

}
上一篇:如何区别: Between Vs. From


下一篇:mybatis 中between and用法