如何获得相对于官方营业时间的时差

时间:2013-10-23 08:43:41

标签: java

我必须显示员工相对于公务时的总工时。 如果任务已启动,则开始输入数据库的时间和结束时间。

输入类似:

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分钟

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

1 个答案:

答案 0 :(得分: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;

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