如何在Joda Time中将一段时间切成几段? [英] How to slice a period of time into several slices in Joda Time?

查看:47
本文介绍了如何在Joda Time中将一段时间切成几段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Java和Joda Time做一个个人日程管理器.对于一个月中的每一天,用户在一天中都有特定的时间段(例如,上午10点至下午4点).他应该能够从自己的一天中提取一个时间段,并添加"一系列琐事(例如弦乐)到该时间段.然后,他将显示一天剩余的时间.他可以从中提取更多的期间,直到没有剩余的期间为止.他可以将提取的时间段添加回当天的时间表,并删除该时间段的所有杂务,即空闲时间.

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

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

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

已添加:用户需要保存整个日程表,以便他可以回来查看过去的工作.每天的时间是可变的-10 am-4pm、9am-1pm等

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

我是Joda Time的新手,我不知道这在Joda Time中是否可行,并且是否可行或麻烦.我只需要一些指导.我不是要为我键入整个代码.

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.

先谢谢了.

推荐答案

这是更新的代码

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更改为

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

运行结果为

8 : 0 : 0
6 : -30 : 0

这篇关于如何在Joda Time中将一段时间切成几段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆