如何计算两个日期之间的工作总小时? [英] How to calculate the total hour worked between two dates?

查看:229
本文介绍了如何计算两个日期之间的工作总小时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个日期,聘请2013年11月19号和endhiring 2014年10月1日,双方将转换为总时间,不考虑周末,但他们有不同的年,因为这样的输出显示:总小时曾是-1200:

 私人诠释calculateTimeInternship(空缺peoplevacancy){
    INT hourWorked = 0;
    日历日期1 = Calendar.getInstance();
    日历日期2 = Calendar.getInstance();

    date1.setTime(peoplevacancy.getDthiring());
    date2.setTime(peoplevacancy.getDtendhiring());

    INT开始= date1.get(Calendar.DAY_OF_YEAR);
    INT端= date2.get(Calendar.DAY_OF_YEAR);

    INT amountDay =(开始 - 结束)+ 1;

    对于(;启动< =结束; INICIO ++){
        如果(date1.get(Calendar.DAY_OF_WEEK)== 1 || date1.get(Calendar.DAY_OF_WEEK)== 7)
        amountDay--;

        date1.add(Calendar.DATE,1);
    }

    hourWorked = amountDay * 4 //每天4小时;
    返回hourWorked;
}
 

解决方案

乔达可以帮助你,但我从来没有能够使用的,因为它的授权。

如果像我一样,乔达是不适合你,你可以解决这个问题,如下所示:

 初始化结束日期对象
初始化的startDate对象
初始化weeksBetween作为
    结束与放大器之间的毫秒;启动/每天毫秒,七(整数楼)划分。
    //可能需要正常化的日期,将它们既午夜或中午或某些共同的时间
初始化daysBetween = weeksBetween * 5 //在任何连续的7天,5是平日。
初始化curDay =的startDate + weeksBetween *7天

而(curDay没有结束日期)
   添加天到curDay
   如果(curDay不是周末)
      daysBetween ++
输出daysBetween * 4
 

您可以让他们之间的毫秒转换为日历的日期(日历有这样的方法来做到这一点)

I have two dates, hiring 11/19/2013 and endhiring 10/01/2014, both are converted to total hours, without considering the weekends, but they have different years and because of this the output says: the total hours worked was -1200:

private int calculateTimeInternship(Vacancy peoplevacancy){
    int hourWorked = 0; 
    Calendar date1 = Calendar.getInstance();  
    Calendar date2 = Calendar.getInstance();    

    date1.setTime(peoplevacancy.getDthiring());  
    date2.setTime(peoplevacancy.getDtendhiring());  

    int initiation = date1.get(Calendar.DAY_OF_YEAR);  
    int end = date2.get(Calendar.DAY_OF_YEAR);  

    int amountDay = (initiation - end) + 1;  

    for (; initiation <= end; inicio++){  
        if (date1.get(Calendar.DAY_OF_WEEK) == 1   || date1.get(Calendar.DAY_OF_WEEK) == 7)  
        amountDay--;  

        date1.add(Calendar.DATE, 1);  
    }

    hourWorked = amountDay * 4 //4 hour per day;
    return hourWorked ;
}

解决方案

Joda can help you, but I'm never able to use it because of its license.

If like me, Joda is not appropriate for you, you can solve this problem as follows:

initialize endDate object
initialize startDate object
initialize weeksBetween as 
    milliseconds between end&start/milliseconds per day, divided by seven (integer floor). 
    //may need to normalize dates and set them to be both midnight or noon or some common time
initialize daysBetween = weeksBetween*5 // in any continuous 7 days, 5 are weekdays.
initialize curDay=startDate + weeksBetween*7 days

while(curDay is not endDate)
   add a day to curDay
   if(curDay is not weekend)
      daysBetween++
output daysBetween* 4

You can get the milliseconds between them by converting the calendars to Date (Calendar has such a method to do this)

这篇关于如何计算两个日期之间的工作总小时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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