5

我有一个“周期”(P)的时间,由开始时间(S)和结束时间(E)表示。我想将 P 分成大小为 D 的 C 块。也就是说,

P = C * D + R,其中 R 是剩余时间或剩余时间。

例如。

S = NOW, E = 10 sec after NOW, D = 3 sec.
Therefore, P = 10 sec, C = 3, R = 1 sec. 

我想存储和显示所有块 C 及其开始时间、结束时间和大小。最后,我想存储和显示剩余部分。我如何使用 Joda Time 做到这一点?

API 是否提供了简单的方法和类来执行此操作,或者我将不得不想出一个出路?

这个问题是我在这里发布的另一个问题的一小部分

4

4 回答 4

11

我不确定这段代码是否是您正在寻找的,但它可能会让您走上正确的轨道。

我假设您有两个 DateTimes 来表示开始日期和结束日期,因为 Joda-Time Period 表示像 1 个月或 2 周这样的时间段。它没有特定的开始或结束,例如表示两个瞬间之间的时间片的间隔。

import java.util.*;
import org.joda.time.*;

class Test {
    public static void main(String... args) {
        DateTime now = new DateTime();
        List<Interval> list = splitDuration(now, now.plusSeconds(10), 3, 3 * 1000);

        for(Interval i : list) {
            System.out.println(i.getStart() + " - " +
                               i.getEnd() + " - " +
                               i.toDurationMillis());
        }
    }

    static List<Interval> splitDuration(DateTime start, DateTime end, long chunkAmount, long chunkSize) {
        long millis = start.getMillis();
        List<Interval> list = new ArrayList<Interval>();

        for(int i = 0; i < chunkAmount; ++i) {
            list.add(new Interval(millis, millis += chunkSize));
        }

        list.add(new Interval(millis, end.getMillis()));
        return list;
    }
}

在我的情况下输出:

2013-03-12T12:29:01.781+01:00 - 2013-03-12T12:29:04.781+01:00 - 3000
2013-03-12T12:29:04.781+01:00 - 2013-03-12T12:29:07.781+01:00 - 3000
2013-03-12T12:29:07.781+01:00 - 2013-03-12T12:29:10.781+01:00 - 3000
2013-03-12T12:29:10.781+01:00 - 2013-03-12T12:29:11.781+01:00 - 1000
于 2013-03-12T11:31:18.913 回答
0

万一有些在给定时间之间看起来相等,这里是代码

`

private List<Interval> splitDateTime(long start, long end, int intervalNo) {
        long interval = (end - start) / intervalNo;
        List<Interval> list = new ArrayList<Interval>();
        for (long i = start + interval; i < end; i += interval) {
            list.add(new Interval(start, i));
            start=start + interval;
        }
        list.add(new Interval(start, end));
        return list;
    }

`

于 2017-11-13T12:38:39.600 回答
0

Kotlin 变体不会丢失最后剩下的块:

/**
 * Splits [Interval] with passed [duration] and returns a list of [Interval]s. The minimum supported duration is a second.
 */
fun Interval.splitBy(duration: Duration): List<Interval> {
    val durationSec = duration.standardSeconds
    val periodSec = toPeriod().toStandardSeconds().seconds
    val chunks = ceil((periodSec / durationSec.toFloat())).toInt()
    var nextStart = start
    return List(chunks) { index ->
        val seconds = if (index < chunks - 1) {
            durationSec
        } else {
            val remainder = periodSec % durationSec
            if (remainder > 0) periodSec % durationSec else durationSec
        }
        Interval(nextStart, nextStart.plusSeconds(seconds.toInt()).also { nextStart = it })
    }
}
于 2020-11-26T22:57:47.910 回答
0

在这种情况下,我最终这样做了:

private static Collection<Interval> splitDuration(Interval interval, int chunks)
{
    long startMillis = interval.getStartMillis();
    long endMillis = interval.getEndMillis();
    long durationMillis = endMillis - startMillis;
    long chunkSize = durationMillis / chunks;

    Collection<Interval> list = new ArrayList<Interval>();
    for (int i = 1; i <= chunks; ++i) {
        list.add(new Interval(startMillis, startMillis += chunkSize));
    }

    return list;
}

要旨

干杯!

于 2016-10-11T09:21:42.700 回答