在Java中的两个工作日之间获取分钟 [英] Get minutes between two working days in Java

查看:106
本文介绍了在Java中的两个工作日之间获取分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算Java中两个Joda DateTimes或Calendars之间的分钟。
当然,如果我可以使用Minutes.minutesBetween函数,这将很简单,但是我只能计算工作日和8:00 AM-6:00 PM之间的分钟数。

I am trying to calculate the minutes between two Joda DateTimes or Calendars in Java. Ofcourse this would be simple if I could use the Minutes.minutesBetween function, but I can only count the minutes between working days and 8:00 AM - 6:00 PM.

例如:


开始日期:2015年3月11日16:00

结束日期:03- 2015年11月11日17:00

Startdate: 03-11-2015 16:00
Enddate: 03-11-2015 17:00

这很简单,因为它恰好是60分钟。因为这是一个工作日,介于给定的工作时间之间。

This would be simple, as it would be exactly 60 minutes. Because it's a workday and between the given work hours.

现在我要计算的是:


开始日期:2015/3/11 17:00

结束日期:2015/03/12 09:00

Startdate: 03-11-2015 17:00
Enddate: 03-12-2015 09:00

这应该返回120分钟,即使它们之间有更多的分钟,因为两个日期之间只有两个工作小时。

This should return 120 minutes even though there's a lot more minutes in between, because there's only two working hours between the two dates.

有时也是两个日期之间的一个周末,也不应添加这些分钟。

Sometimes there's also a weekend between two dates, those minutes should not be added either.

我一直在不懈地努力实现这一目标,但是我没有找到解决方案。

I have been trying tirelessly to implement this, but I have not found a solution.

有什么想法吗?

预先感谢。

编辑:
由于戴维斯·布罗达(Davis Broda)的伪代码,我找到了一个解决方案:

Thanks to Davis Broda's pseudocode I have found a solution:

private static int BEGINWORKHOUR = 8;
private static int ENDWORKHOUR = 16;

public int getWorkedMinutes(){
    Calendar start = (Calendar) this.startTime.clone();
    Calendar end = (Calendar) this.endTime.clone();

    if(start.get(Calendar.HOUR_OF_DAY) < BEGINWORKHOUR){
        start.set(Calendar.HOUR_OF_DAY, BEGINWORKHOUR);
        start.set(Calendar.MINUTE, 0);
    }

    if(end.get(Calendar.HOUR_OF_DAY) >= ENDWORKHOUR){
        end.set(Calendar.HOUR_OF_DAY, ENDWORKHOUR);
        end.set(Calendar.MINUTE, 0);
    }

    int workedMins = 0;
    while(!sameDay(start, end)){
        workedMins += workedMinsDay(start);
        start.add(Calendar.DAY_OF_MONTH, 1);
        start.set(Calendar.HOUR_OF_DAY, BEGINWORKHOUR);
        start.set(Calendar.MINUTE, 0);
    }
    workedMins += (end.get(Calendar.MINUTE) - start.get(Calendar.MINUTE)) + ((end.get(Calendar.HOUR_OF_DAY) - start.get(Calendar.HOUR_OF_DAY))*60);
    return workedMins;
}

private int workedMinsDay(Calendar start){
    if((start.get(Calendar.DAY_OF_WEEK) == 1) || (start.get(Calendar.DAY_OF_WEEK) == 6))    return 0;
    else return (60 - start.get(Calendar.MINUTE)) + ((ENDWORKHOUR - start.get(Calendar.HOUR_OF_DAY) - 1)*60);
}

private boolean sameDay(Calendar start, Calendar end){
    if(start.get(Calendar.YEAR) == end.get(Calendar.YEAR) && start.get(Calendar.MONTH) == end.get(Calendar.MONTH) &&
            start.get(Calendar.DAY_OF_MONTH) == end.get(Calendar.DAY_OF_MONTH)) return true;
    else return false;
}


推荐答案

伪代码解决方案可能看起来像像下面这样。

a pseudocode solution might look something like the below.

onSameWorkDay?

    if yes
        simple date comparison
    if no
        days = find number of whole work days between the two dates
        dayToMin = days * 8 * 60
        minDay1 = find minutes between specified start time and end of day1
        minDay2 = find minutes between start of final day and specified end time
        totalTime = dayToMin + minDay1 + minDay2

这篇关于在Java中的两个工作日之间获取分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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