当前位置: 首页 > 知识库问答 >
问题:

如何用约达时间把一段时间切成若干片?

张德佑
2023-03-14

共有1个答案

龙佐
2023-03-14

这是更新后的代码

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
 类似资料:
  • 问题内容: 是否可以使用Jodatime生成随机日期时间,以使日期时间的格式为yyyy-MM-dd HH:MM:SS,并且应该能够生成两个随机日期时间,其中Date2减去Date1会大于2分钟但小于60分钟 请提出一些方法。 问题答案: 这严格遵循您的要求(更正的格式除外)。 如果运行此命令,您会注意到多年来您将获得令人发指的值,但这仅仅是在DateTime整个可能范围(或该日期的Date)上生成

  • 我使用获取给定时间段s的天数。 它似乎对大多数情况都有效,但是整整一个月,我有一段时间是零天。 以下测试用例失败(

  • rank ▲ ✰ vote url 67 356 78 226 url 把字符串转化成时间 短小精悍.我有一个特别大的列表存储了下面的字符串: Jun 1 2005 1:33PM Aug 28 1999 12:00AM 我想把它们转化成合适的时间格式存进数据库,所以我需要把它们变成真正的时间对象. 非常感谢帮助. 检查time模块的strptime函数.它是strftime的转换. from

  • 询问代码的问题必须表明对正在解决的问题的理解程度最低。。包括尝试的解决方案、它们不起作用的原因以及预期的结果。另请参见:堆栈溢出问题检查表 似乎有很多信息将一个时间段转换为一个时间,但不是相反。 我需要做的一个例子是把120分钟转换成P0DT2H0M0S。和13:10进入P0DT13H10M0S。以及进入PT2H0M0S的120分钟。 有什么快速简单的方法吗?

  • "2023-07-31T02:36:31.000+00:00" 这个时间格式如何转成 '2023-07-31 10:36:31' 这个时间

  • 问题内容: 我从日志文件中获取了一些数据,想按分钟对条目进行分组: df: 我尝试了以下选项: 级别太高,想要汇总。 失败失败。 工作正常,并返回一个对象进行进一步处理,例如: grouped.Source.value_counts() 2012-01-01 12:30:00 source1 1 2012-01-01 12:31:00 source2 2 source1 2 2012-01-01