如何在不考虑增加周末的情况下将“工作时间"添加到“日期"? -Java [英] How to add Business hours to Date considering not adding weekends ? - Java

查看:70
本文介绍了如何在不考虑增加周末的情况下将“工作时间"添加到“日期"? -Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一些至今的小时数,而忽略周末

I want to add certain number of hours to date, ignoring the weekends

例如

(星期五18:00)+ 48 =(星期二18:00)(星期六和星期日被忽略)

(Friday 18:00) + 48 = (Tuseday 18:00) (Saturday and Sunday are ignored)

由于公司24小时工作,所以工作时间为24.但是我仍然无法获得仅在工作日增加工作时间的方法

since the company works 24 hours, business hours are 24. But still i could not get how to add hours only on business days

功能可能类似于:

public Date getTaskEndTime(Calendar startDate, int hours){
   // calculate the end time by adding the hours ignoring the weekends
}

推荐答案

添加的小时数不得超过24小时.并在每个步骤之后检查是否在星期六或星期天结束.在每种情况下,都需要增加24小时.那应该做你想要的.

Add hours in steps not bigger then 24hours. And check after each step if you end up on a saturday or sunday. In each case add another 24hours. That should do what you want.

public Date getTaskEndTime(Calendar startDate, int hours){
    while (hours > 0){
        int step = 0;
        if(hours > 24) step = 24;
        else step = hours;          
        hours -= step;          
        startDate.add(Calendar.HOUR_OF_DAY, step);          
        int dayOfWeek = startDate.get(Calendar.DAY_OF_WEEK);
        if(dayOfWeek == Calendar.SATURDAY) hours += 24;
        if(dayOfWeek == Calendar.SUNDAY) hours += 24;
    }
    return startDate.getTime();
}

这篇关于如何在不考虑增加周末的情况下将“工作时间"添加到“日期"? -Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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