Dividing a Joda Time Period into intervals of desired size?

五迷三道 提交于 2019-12-10 18:32:25

问题


I have a "period" (P) of time that is represented by a start time (S) and an end time (E). I want to divide P into C chunks of size D. That is,

P = C * D + R, where R is the remainder or leftover time.

eg.

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

I want to store and display all the chunks C with their start times, end times and sizes. Finally, I want to store and display the remainder. How do I do this using Joda Time ?

Does the API provide simple methods and classes to do this or I will have to figure a way out ?

This question is a small part of another question I have posted here


回答1:


I'm not sure whether this code is what you're looking for but it might get you on the right track.

I assumed you have two DateTimes to represent the start and end dates, because a Joda-Time Period represents a period of time like 1 month or 2 weeks. It doesn't have a specific start or end like, for example an Interval which represents a slice of time between two instants.

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;
    }
}

Output in my case:

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



回答2:


In this case, I've ended up by doing this:

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;
}

Gist

Cheers!




回答3:


In Case some is looking equal denominator between given time here is code

`

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;
    }

`



来源:https://stackoverflow.com/questions/15358409/dividing-a-joda-time-period-into-intervals-of-desired-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!