0

我必须显示员工相对于正式工作时间的总工作时间。如果启动了任务,则将开始时间和结束时间输入到数据库中。

输入类似:

start time= `2013-10-18 16:06:16.840`
end time=`2013-10-22 16:09:09.760`

工作时间可根据组织进行配置。

工作时间= 7.45 (7 hour and 45 minutes) =1 day

如果某人的工作时间超过工作时间,则视为加班并视为加班。

我需要根据用户工作一天的输出。一天的输出就像

7.45  hour then 1 days 
7.50 hour then 1 days and 5 minute
14.50 hour then 1 days, 7 hour and 5 minutes

我已经完成了以下编码。它没有给出我期望的准确结果。

我做了什么:

1) take difference of two days
2) instead of using 24 hour, I used business hour.

代码:

 public static long[] getTimeDifference(Date d1,Date d2,int holiday,String businessHour)
    {
    long hour=0;
     if(businessHour.indexOf(".")!=-1)
     {
     hour=Long.valueOf(businessHour.substring(0,businessHour.indexOf(".")));///check for hour
     }
     else
     {
     hour=Long.valueOf(businessHour);
     }
     long[] time=new long[3];
     long diff = d2.getTime() - d1.getTime();
                     
     //double diffSeconds = diff / 1000 % 60;
     long diffMinutes = diff / (60 * 1000) % 60;
     long diffHours =  (diff / (60 * 60 * 1000) % hour);
     long diffDays =   (diff / (hour * 60 * 60 * 1000));
    diffDays=diffDays-holiday;
     time[0]=diffDays;
     time[1]=diffHours;
     time[2]=diffMinutes;
                     
     return time;
    }

输出: 11 天: 05 小时: 02 分钟

我还没有包含分钟逻辑,我该如何执行?我不知道。我对此感到困惑。

4

1 回答 1

0

要获取两个日期之间经过的天数、分钟数和秒数,您可以执行以下操作:

    long hour = 0;
    if (businessHour.indexOf(".") != -1) {
        hour = Long.valueOf(businessHour.substring(0,
            businessHour.indexOf(".")));// /check for hour
    } else {
        hour = Long.valueOf(businessHour);
    }
    long[] time = new long[3];
    long diff = d2.getTime() - d1.getTime();

    // double diffSeconds = diff / 1000 % 60;

    double diffDays = (diff / (24 * 60 * 60 * 1000));
    double diffHours = (diff / (60 * 60 * 1000)) - diffDays*24;
    double diffMinutes = diff / (60 * 1000) - (diffDays*24 + diffHours) * 60;

    time[0] = diffDays;
    time[1] = diffHours;
    time[2] = diffMinutes;

但不知道是不是你想要的。

于 2013-10-23T09:24:20.443 回答