Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 计算两个时隙共有的时间_Java_Time - Fatal编程技术网

Java 计算两个时隙共有的时间

Java 计算两个时隙共有的时间,java,time,Java,Time,我需要计算两个时隙共有的时间。我想知道是否有一种有效的计算方法。我知道用int来节省时间不是最好的方法。有人能给我一个新的方法来保存它和计算它吗 我需要计算两个可用时间段重叠的持续时间。例如,如果我有第一个可用的时间段,即周四10:00到13:00,以及周四12:00到14:00,那么我们可以计算这两个时间段的可用时间 public class TimeSlot implements Cloneable { int day; int hourStart; int hou

我需要计算两个时隙共有的时间。我想知道是否有一种有效的计算方法。我知道用int来节省时间不是最好的方法。有人能给我一个新的方法来保存它和计算它吗

我需要计算两个可用时间段重叠的持续时间。例如,如果我有第一个可用的时间段,即周四10:00到13:00,以及周四12:00到14:00,那么我们可以计算这两个时间段的可用时间

public class TimeSlot implements Cloneable {

    int day;
    int hourStart;
    int hourEnd;
    int minutesStart;
    int minutesEnd;
    int id;

    public TimeSlot(int day, int hourStart, int hourEnd, int minutesStart, int minutesEnd) {
        this.day = day;
        this.hourStart = hourStart;
        this.hourEnd = hourEnd;
        this.minutesStart = minutesStart;
        this.minutesEnd = minutesEnd;
        this.id = AutoIDGenerator.getAutoIdTimeSlot();

    }

    @Override
    protected TimeSlot clone() throws CloneNotSupportedException {
        return (TimeSlot) super.clone();
    }

    public boolean isTheSameDay(TimeSlot t) {
        if (this.getDay() == t.getDay()) {
            return true;
        }
        return false;
    }

    /**
     *
     * @param t
     * @return si l'heure fournie est après this timeslot
     */
    public boolean isAfter(TimeSlot t) {
        if (this.isTheSameDay(t)) {
            if (this.getHourEnd() > t.getHourStart()) {
                return true;
            } else if (this.getHourEnd() == t.getHourStart() && this.getMinutesEnd() > t.getMinutesStart()) {
                return true;
            }

        }
        return false;
    }

    /**
     *
     * @param t
     * @return si l'heure fournie est avant this timeslot
     */
    public boolean isBefore(TimeSlot t) {
        if (this.isTheSameDay(t)) {
            if (this.getHourStart() > t.getHourEnd()) {
                return true;
            } else if ((this.getHourStart() == t.getHourEnd()) && (this.getMinutesStart() > t.getMinutesEnd())) {
                return true;
            }
        }
        return false;
    }

    public boolean isCompatible(TimeSlot t) {
        if (!(isBefore(t)) && !(isAfter(t))) {
            return true;
        }
        return false;

    }
}

我认为解决这个问题最好的方法是使用日期, 您需要定义4个日期,它们是: 事件A开始时间、事件A结束时间、事件B开始时间、事件B结束时间、

然后检查这些日期是否有重叠

如果是的话,计算多少(以毫秒为单位)并最终将以毫秒为单位的差值格式化为HH:mm:ss格式

例子:
这个例子产生了这样的结果

时间重叠为:01:00:00

在定义的2个事件之间


我希望我得到了一个问题:)

最好的方法是使用java.util.Calendar类

您的时间段可以定义为开始时间和结束时间的两个日历值。 您可以使用日历时间的毫秒数进行操作(使用getTimeInMillis()方法)


使用java.util.Calendar作为时间段,您可以获得任何精度(天、小时、分钟…)的解决方案

如果您可以使用joda time,它有一个with,可以完全满足您的需要

第二个最佳解决方案是使用适当的日期表示法,如果使用Java 7或更早版本,则使用
日期
,如果使用Java 8,则使用
即时

使用Java 8,您的类可以如下所示:

class Timeslot {
  Instant start, end;

  Duration overlap(Timeslot other) {
    long startOverlap = Math.max(this.start.toEpochMilli(), other.start.toEpochMilli());
    long endOverlap = Math.min(this.end.toEpochMilli(), other.end.toEpochMilli());
    if (endOverlap <= startOverlap) return Duration.ofMillis(0); //or a negative duration?
    else return Duration.ofMillis(endOverlap - startOverlap);
  }
}
类时隙{
瞬间开始,瞬间结束;
持续时间重叠(时隙其他){
long startOverlap=Math.max(this.start.toEpochMilli(),other.start.toEpochMilli());
long endOverlap=Math.min(this.end.toEpochMilli(),other.end.toEpochMilli());

如果例如,如果我有一个星期四10点到13点的第一个时间段和另一个星期四12点到2点的第二个时间段,我们可以计算出我们两个都有多少时间可以利用。这就是为什么我需要补充一点
class Timeslot {
  Instant start, end;

  Duration overlap(Timeslot other) {
    long startOverlap = Math.max(this.start.toEpochMilli(), other.start.toEpochMilli());
    long endOverlap = Math.min(this.end.toEpochMilli(), other.end.toEpochMilli());
    if (endOverlap <= startOverlap) return Duration.ofMillis(0); //or a negative duration?
    else return Duration.ofMillis(endOverlap - startOverlap);
  }
}