java - 如何在 Joda Time 中将一段时间分割成多个片段?

标签 java jodatime

我想使用 Java 和 Joda Time 制作一个个人日程管理器。对于一个月中的每一天,用户在一天中都有特定的时间段(例如上午 10 点至下午 4 点)。他应该能够从一天中提取一个时间段,并将一系列杂务(即字符串)“添加”到该时间段中。然后他将看到一天的剩余时间。他可以从中提取更多的时间段,直到没有剩余的时间段为止。他可以将提取的时间段添加回当天的日程安排中,并删除该时间段内的所有杂务,即空闲时间。

可以提取的时间片的大小由用户决定。时间片数量=一天可用总时间/用户设置的时间片大小=所需大小的时间片数量+剩余时间

他可以将一天分为 1 小时、30 分钟、15 分钟、20 分钟、40 分钟等。

添加:用户需要保存他的整个日程安排,以便他可以回来检查他过去做了什么。每天的工作时间可以变化 - 上午 10 点至下午 4 点、上午 9 点至下午 1 点等

我是 Joda Time 的新手,我不知道这在 Joda Time 中是否可行,如果可以的话,是简单还是麻烦。我只需要一些指导。我并不要求为我输入整个代码。

提前致谢。

最佳答案

这是更新后的代码

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

这次运行的输出是

 8 : 0 : 0
 5 : 0 : 0

但是,如果将firstEvent更改为

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

运行的输出是

8 : 0 : 0
6 : -30 : 0

关于java - 如何在 Joda Time 中将一段时间分割成多个片段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15355090/

相关文章:

java - 为什么我的程序连续输出两次结果?

java - 提取 XML 标签之间的内容

java - 是否可以使用 Java 截断和启用 HBase 表?

java - JDBC、用户定义对象和用户定义对象的数组列表

java - 单元格渲染器不格式化时间戳

java - 在 Joda-Time 间隔中查找漏洞

java - Joda-Time : convert SimpleDateFormat pattern to DateTimeFormat. forPattern

java - 我想在这个 block 中转换为java 8流?

java - Joda 持续时间转换为最接近的上限

java - 按日期比较 DateTime 对象