编程语言
首页 > 编程语言> > java-如何检查日期时间是使用Joda Time重复发生的事件?

java-如何检查日期时间是使用Joda Time重复发生的事件?

作者:互联网

我有一个DateTime,表示重复事件的开始.天(每日周期)将代表重复周期.我认为这个重复发生的事件将永远不会停止.

from = "2013-06-27"  
period = 3 days
nextOccurence will be "2013-06-30", "2013-07-03", "2013-07-06", and so on.
"2013-07-03" is an occurence but "2013-07-04" isn't an occurence.

我想知道在性能方面确定DateTime是否发生重复事件的最佳方法是什么?从长远来看,该程序将需要检查是否出现了“ 2014-07-03”或“ 2015-07-03”.

解决方法:

这将在我的计算机上在180毫秒的时间内运行所有五次检查.大约每秒27次检查,即您要检查未来300年的日期.

@Test
public void isOccurrence() {
    long startTime = System.currentTimeMillis();

    assertTrue(isOccurrence(new DateMidnight(2010, 1, 10), 3, new DateTime(2010, 1, 19, 0, 0)));
    assertFalse(isOccurrence(new DateMidnight(2010, 1, 10), 3, new DateTime(2010, 1, 18, 0, 0)));

    assertTrue(isOccurrence(new DateMidnight(2010, 1, 10), 3, new DateTime(2310, 1, 19, 0, 0)));
    assertFalse(isOccurrence(new DateMidnight(2010, 1, 10), 3, new DateTime(2310, 1, 20, 0, 0)));

    assertTrue(isOccurrence(new DateMidnight(2010, 1, 10), 3, new DateTime(2010, 1, 10, 0, 0)));

    System.out.println("elapsed=" + (System.currentTimeMillis() - startTime));
}

public boolean isOccurrence(DateMidnight startDate, int dayIncrement, DateTime testTime) {
    DateMidnight testDateMidnight = testTime.toDateMidnight();
    while (startDate.isBefore(testDateMidnight)) {
        startDate = startDate.plusDays(dayIncrement);
    }
    return startDate.equals(testDateMidnight);
}

标签:java,performance,jodatime,time
来源: https://codeday.me/bug/20191013/1907184.html