joda时间:计算到下一个完整时间间隔的时间

fhg3lkii  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(312)

我正在实现一个应用程序,用户可以创建具有到期日期的任务并重复它们,例如,每x天重复一次,每x周重复一次,每x月重复一次等等。
当任务到期时,我想根据定义的时间间隔计算下一个(重复的)到期日期。我的实现在几周内有效,但在几个月内无效,因为我无法将月份转换为分钟。
alarmhelper.java文件

// Works
static LocalDateTime getNextWeeklyIntervalDate(LocalDateTime creationTime, LocalDateTime dueTime, int intervalInWeeks) {
    int intervalInMinutes = Weeks.weeks(intervalInWeeks).toStandardMinutes().getMinutes();
    int timePassed = Minutes.minutesBetween(creationTime, dueTime).getMinutes();
    int rest = timePassed % intervalInMinutes;
    int minutesToNextFullInterval = intervalInMinutes - rest;
    return dueTime.plusMinutes(minutesToNextFullInterval );
}

// How to implement this analogous to weekly, but for monthly intervals? toStandardMinutes() does not exist for months  
static LocalDateTime getNextMonthlyIntervalDate(LocalDateTime creationTime, LocalDateTime dueTime, int intervalInMonth) {
    int intervalInMinutes = Months.months(intervalInMonth).toStandardMinutes().getMinutes(); // toStandardMinutes() does not exist
}

alarmhelpertest.java文件

// User created the task on 01.01.2021 (January 1st, 2021) and want to repeat it weekly.
// So the intervals are 08.01.2021, 15.01.2021 etc.
// In the test the task is due on 12.01.2021, which means the next weekly interval is fulfilled on 15.01.2021.

//The test passes
 @Test
 public void TestGetNextWeeklyIntervalDate() {
    LocalDateTime creation = createLocalDateTime("01.01.2021 06:30:00");
    LocalDateTime due = createLocalDateTime("12.01.2021 05:30:00");
    LocalDateTime expected = createLocalDateTime("15.01.2021 06:30:00");
    LocalDateTime result = AlarmHelper.getNextWeeklyIntervalDate(creation, due, 1);
    assertEquals(expected, result);
}
ovfsdjhp

ovfsdjhp1#

注意:请查看joda time主页上的以下通知
joda time是javase8之前的java事实上的标准日期和时间库。现在要求用户迁移到java.time(jsr-310)。
因此,我建议你用 java.time api使用以下方法: LocalDateTime#isBefore LocalDateTime#plusWeeks LocalDateTime#plusMonths 使用 java.time 应用程序编程接口:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        LocalDateTime creation = createLocalDateTime("01.01.2021 06:30:00");
        LocalDateTime due = createLocalDateTime("12.01.2021 05:30:00");
        System.out.println(getNextWeeklyIntervalDate(creation, due, 1));
        System.out.println(getNextMonthlyIntervalDate(creation, due, 1));
    }

    static LocalDateTime getNextWeeklyIntervalDate(LocalDateTime creationTime, LocalDateTime dueTime,
            int intervalInWeeks) {
        LocalDateTime ldt = creationTime;
        while (ldt.isBefore(dueTime)) {
            ldt = ldt.plusWeeks(intervalInWeeks);
        }
        return ldt;
    }

    static LocalDateTime getNextMonthlyIntervalDate(LocalDateTime creationTime, LocalDateTime dueTime,
            int intervalInMonth) {
        LocalDateTime ldt = creationTime;
        while (ldt.isBefore(dueTime)) {
            ldt = ldt.plusMonths(intervalInMonth);
        }
        return ldt;
    }

    static LocalDateTime createLocalDateTime(String dateTimeStr) {
        return LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss", Locale.ENGLISH));
    }
}

输出:

2021-01-15T06:30
2021-02-01T06:30

这些方法也可以在joda time api中使用,也就是说,您所需要更改的只是日期时间字符串的解析方式 LocalDateTime 获得。
使用joda时间api:

import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        LocalDateTime creation = createLocalDateTime("01.01.2021 06:30:00");
        LocalDateTime due = createLocalDateTime("12.01.2021 05:30:00");
        System.out.println(getNextWeeklyIntervalDate(creation, due, 1));
        System.out.println(getNextMonthlyIntervalDate(creation, due, 1));
    }

    static LocalDateTime getNextWeeklyIntervalDate(LocalDateTime creationTime, LocalDateTime dueTime,
            int intervalInWeeks) {
        LocalDateTime ldt = creationTime;
        while (ldt.isBefore(dueTime)) {
            ldt = ldt.plusWeeks(intervalInWeeks);
        }
        return ldt;
    }

    static LocalDateTime getNextMonthlyIntervalDate(LocalDateTime creationTime, LocalDateTime dueTime,
            int intervalInMonth) {
        LocalDateTime ldt = creationTime;
        while (ldt.isBefore(dueTime)) {
            ldt = ldt.plusMonths(intervalInMonth);
        }
        return ldt;
    }

    static LocalDateTime createLocalDateTime(String dateTimeStr) {
        DateTimeFormatter dtf = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm:ss");
        LocalDateTime ldt = dtf.parseDateTime(dateTimeStr).toLocalDateTime();
        return ldt;
    }
}

输出:

2021-01-15T06:30:00.000
2021-02-01T06:30:00.000

正如你所见,除了里面的代码 createLocalDateTime ,两者相同 java.time 和joda时间api。

相关问题