Java 计算事件的下一个日期

Java 计算事件的下一个日期,java,date,events,date-manipulation,Java,Date,Events,Date Manipulation,我正在尝试构建一个具有重复事件的应用程序。 在我的数据库中,我有一个表模式: type_name sepereation_count day_of_week week_of_month day_of_month month_of_year daily 0 NULL NULL NULL NULL weekly 0 2 NULL

我正在尝试构建一个具有重复事件的应用程序。 在我的数据库中,我有一个表模式:

type_name  sepereation_count  day_of_week  week_of_month  day_of_month  month_of_year
daily      0                  NULL         NULL           NULL          NULL
weekly     0                  2            NULL           NULL          NULL
monthly    0                  1            3              NULL          NULL
monthly2   1                  NULL         NULL           10            NULL
yearly     0                  NULL         NULL           20            5
分离计数:如果需要每隔一周/月/等配置一次事件,则分离计数将为1
每周的第天:1=星期一;2=周二等

我有一张表,上面有重复发生的事件:

id  start_date  last_occurence  recurring_pattern_name
1   2019-10-01  2019-12-03      daily
2   2019-10-01  2019-12-03      weekly
3   2019-10-01  2019-11-18      monthly
4   2019-11-01  NULL            monthly2
5   2019-01-01  2019-05-20      yearly
活动的下一个日期应为:
1=2019-12-04因为每天 2=2019-12-10,因为每周周二(本周从周一开始)
3=2019-12-16,因为每3年。星期一的一周
4=2019-12-10,因为每两个月10日
5=2020-05-20,因为每年第5个月和第20天

现在我正试图用Java寻找下一个日期,但我不知道如何才能做到这一点
为了我每天的活动

nextDate = lastPaid == null ? startDate : lastPaid;
nextDate = nextDate.plusDays(1 + recurringPattern.getSepereationCount());
但是,我如何获得每周、每月、每月和每年活动的下一个日期


每周计划

    int separationCount = 0;
    LocalDate start = LocalDate.parse("2019-10-01");
    int dowNumber = 2;

    DayOfWeek dow = DayOfWeek.of(dowNumber);

    LocalDate first = start.with(TemporalAdjusters.nextOrSame(dow));
    System.out.println("first: " + first);
    LocalDate next = first.plusWeeks(1 + separationCount);
    System.out.println("next: " + next);
    LocalDate start = LocalDate.parse("2019-10-01");
    int dowNumber = 1;
    int weekOfMonth = 3;

    DayOfWeek dow = DayOfWeek.of(dowNumber);

    LocalDate first = start.with(WeekFields.ISO.weekOfMonth(), weekOfMonth)
            .with(dow);
    if (first.isBefore(start)) {
        // Use next month instead
        first = start.plusMonths(1)
                .with(WeekFields.ISO.weekOfMonth(), weekOfMonth)
                .with(dow);
    }

    System.out.println("first: " + first);
此演示片段的输出为:

月度计划

    int separationCount = 0;
    LocalDate start = LocalDate.parse("2019-10-01");
    int dowNumber = 2;

    DayOfWeek dow = DayOfWeek.of(dowNumber);

    LocalDate first = start.with(TemporalAdjusters.nextOrSame(dow));
    System.out.println("first: " + first);
    LocalDate next = first.plusWeeks(1 + separationCount);
    System.out.println("next: " + next);
    LocalDate start = LocalDate.parse("2019-10-01");
    int dowNumber = 1;
    int weekOfMonth = 3;

    DayOfWeek dow = DayOfWeek.of(dowNumber);

    LocalDate first = start.with(WeekFields.ISO.weekOfMonth(), weekOfMonth)
            .with(dow);
    if (first.isBefore(start)) {
        // Use next month instead
        first = start.plusMonths(1)
                .with(WeekFields.ISO.weekOfMonth(), weekOfMonth)
                .with(dow);
    }

    System.out.println("first: " + first);
您可以用类似的方式计算以下日期

月2计划

    int separationCount = 0;
    LocalDate start = LocalDate.parse("2019-10-01");
    int dowNumber = 2;

    DayOfWeek dow = DayOfWeek.of(dowNumber);

    LocalDate first = start.with(TemporalAdjusters.nextOrSame(dow));
    System.out.println("first: " + first);
    LocalDate next = first.plusWeeks(1 + separationCount);
    System.out.println("next: " + next);
    LocalDate start = LocalDate.parse("2019-10-01");
    int dowNumber = 1;
    int weekOfMonth = 3;

    DayOfWeek dow = DayOfWeek.of(dowNumber);

    LocalDate first = start.with(WeekFields.ISO.weekOfMonth(), weekOfMonth)
            .with(dow);
    if (first.isBefore(start)) {
        // Use next month instead
        first = start.plusMonths(1)
                .with(WeekFields.ISO.weekOfMonth(), weekOfMonth)
                .with(dow);
    }

    System.out.println("first: " + first);
这件事很琐碎。使用
LocalDate.withDayOfMonth()
LocalDate.plusMonths()
。请记住,以与上面相同的方式检查您计算的第一个日期是否早于开始日期。记住,第29、30或31天不是每个月都有效的

年度计划

    int separationCount = 0;
    LocalDate start = LocalDate.parse("2019-10-01");
    int dowNumber = 2;

    DayOfWeek dow = DayOfWeek.of(dowNumber);

    LocalDate first = start.with(TemporalAdjusters.nextOrSame(dow));
    System.out.println("first: " + first);
    LocalDate next = first.plusWeeks(1 + separationCount);
    System.out.println("next: " + next);
    LocalDate start = LocalDate.parse("2019-10-01");
    int dowNumber = 1;
    int weekOfMonth = 3;

    DayOfWeek dow = DayOfWeek.of(dowNumber);

    LocalDate first = start.with(WeekFields.ISO.weekOfMonth(), weekOfMonth)
            .with(dow);
    if (first.isBefore(start)) {
        // Use next month instead
        first = start.plusMonths(1)
                .with(WeekFields.ISO.weekOfMonth(), weekOfMonth)
                .with(dow);
    }

    System.out.println("first: " + first);
我把这个留给你。在
LocalDate
的文档中找到所需内容

文档链接