How to slice a period of time into several slices in Joda Time?

牧云@^-^@ 提交于 2019-12-12 18:49:12

问题


I want to make a personal schedule manager using Java and Joda Time. The user has a certain period in a day (eg. 10am-4pm) , for each day in a month. He should be able to extract a period from his day and "add" a list of chores (ie strings) to this period. Then he will be shown the remaining portion of his day. He can extract more periods from this until he has no time periods left. He can add an extracted period back to the schedule for the day and remove all chores for that period, that is free time.

The size of a time slice that can be extracted is decided by the user. number of time slices = total time available in a day/size of time-slice set by user = num of time slices of desired size + remainder time

He can divide his day into chunks of 1hr, 30 mins, 15 mins, 20mins, 40mins etc.

ADDED: The user needs to have his whole schedule saved so that he can come back and check what he was doing in the past. The hours each day can be variable - 10am-4pm, 9am-1pm etc

I am new to Joda Time and I don't know if this is possible in Joda Time and if possible, is it easy or cumbersome. I only need some guidance. I am not asking for the whole code to be typed for me.

Thanks in advance.


回答1:


here's the updated code

import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;
import org.joda.time.Period;

public class Schedule {

// figure out some way to Store these values in a database
static List<DateTime[]> schedule = new ArrayList<DateTime[]>();

//Number of hours in a day
static DateTime startDay = new DateTime(2013, 03, 12, 8, 0, 0);
static DateTime endDay = new DateTime(2013, 03, 12, 16, 0, 0);

//Create a Period from the start and End Day dates
static Period dayPeriod = new Period(startDay, endDay);

public static void main(String[] args) {

    // These events will be created on your UI
    DateTime[] firstEvent = { new DateTime(2013, 03, 12, 9, 0, 0), new DateTime(2013, 03, 12, 10, 0, 0) };
    DateTime[] secEvent = { new DateTime(2013, 03, 12, 11, 0, 0), new DateTime(2013, 03, 12, 12, 0, 0) };
    DateTime[] thirdEvent = { new DateTime(2013, 03, 12, 12, 0, 0), new DateTime(2013, 03, 12, 13, 0, 0) };

    //print the hours left, before creating events
    System.out.println(dayPeriod.getHours() + " : " + dayPeriod.getMinutes() + " : " + dayPeriod.getSeconds());

    //Call a method to validate them
    validateAndAdd(firstEvent);
    validateAndAdd(secEvent);
    validateAndAdd(thirdEvent);

    //print the hours left after creating events
    System.out.println(dayPeriod.getHours() + " : " + dayPeriod.getMinutes() + " : " + dayPeriod.getSeconds());

}

public static void validateAndAdd(DateTime[] event) {

    //Subtract the event period from the Day Period
    dayPeriod = dayPeriod.minus(new Period(event[0], event[1]));
    schedule.add(event);
}
}

output from this run is

 8 : 0 : 0
 5 : 0 : 0

however, if you change the firstEvent to

DateTime[] firstEvent = { new DateTime(2013, 03, 12, 9, 30, 0), new DateTime(2013, 03, 12, 10, 0, 0) };

output from the run is

8 : 0 : 0
6 : -30 : 0



回答2:


Method split start and end in given interval.

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



回答3:


To achieve what you describe, you need to caculate the chunk length and store begin time and end time for each chunk. Through JodaTime, it is every easy to calculate a chunk's begin time and end time. It provides API like DateTime#plusHours (add N hours), DateTime#plusMinutes (add N minutes).

For example:

DateTime beginTime = DateTime.now();
DateTime endTime = beginTime.plusMinutes(40);
System.out.println(beginTime);
System.out.println(endTime);


来源:https://stackoverflow.com/questions/15355090/how-to-slice-a-period-of-time-into-several-slices-in-joda-time

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