欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java计算工作时间除去节假日以及双休日

程序员文章站 2023-12-17 16:05:58
本文实例为大家分享了java计算工作时间的具体代码,不包括节假日、双休日,供大家参考,具体内容如下 package common.util; import j...

本文实例为大家分享了java计算工作时间的具体代码,不包括节假日、双休日,供大家参考,具体内容如下

package common.util; 
import java.text.dateformat; 
import java.text.parseexception; 
import java.text.simpledateformat; 
import java.util.arraylist; 
import java.util.calendar; 
import java.util.date; 
import java.util.linkedlist; 
import java.util.list; 
 
public class calculatehours { 
  simpledateformat format = new simpledateformat( "yyyy-mm-dd hh:mm:ss"); //这里的格式可以自己设置  
  //设置上班时间:该处时间可以根据实际情况进行调整 
  int abh = 9;//上午上班时间,小时 
  int abm = 00;//上午上班时间,分钟 
  int aeh = 12;//上午下班时间,小时 
  int aem = 0;//上午下班时间,分钟 
  int pbh = 13;//下午上班时间,小时 
  int pbm = 00;//下午上班时间,分钟 
  int peh = 18;//下午下班时间,小时 
  int pem = 0;//下午下班时间,分钟 
  float h1 = abh+(float)abm/60; 
  float h2 = aeh+(float)aem/60; 
  float h3 = pbh+(float)pbm/60; 
  float h4 = peh+(float)pem/60; 
  float hoursperday = h2-h1+(h4-h3);//每天上班小时数 
  int daysperweek = 5;//每周工作天数 
  long milsecperday = 1000*60*60*24;//每天的毫秒数 
  float hoursperweek = hoursperday*daysperweek;//每星期小时数 
  public float calculatehours(string begintime, string endtime){ 
    //对输入的字符串形式的时间进行转换 
    date t1 = stringtodate(begintime);//真实开始时间 
    date t2 = stringtodate(endtime);//真实结束时间 
    //对时间进行预处理 
    t1 = processbegintime(t1); 
    t2 = processendtime(t2); 
    //若开始时间晚于结束时间,返回0 
    if(t1.gettime()>t2.gettime()){ 
      return 0; 
    } 
    //开始时间到结束时间的完整星期数 
    int weekcount = (int) ((t2.gettime()-t1.gettime())/(milsecperday*7)); 
    float totalhours = 0; 
    totalhours += weekcount * hoursperweek; 
    //调整结束时间,使开始时间和结束时间在一个星期的周期之内 
    t2.settime(t2.gettime()-weekcount*7*milsecperday); 
    int daycounts = 0;//记录开始时间和结束时间之间工作日天数 
    //调整开始时间,使得开始时间和结束时间在同一天,或者相邻的工作日内。 
    while(t1.gettime()<=t2.gettime()){ 
      date temp = new date(t1.gettime()+milsecperday); 
      temp = processbegintime(temp); 
      temp.sethours(t1.gethours()); 
      temp.setminutes(t1.getminutes()); 
      if(temp.gettime()>t2.gettime()){ 
        break; 
      }else{ 
        t1 = temp; 
        daycounts++; 
      } 
    } 
    totalhours += daycounts * hoursperday; 
    float hh1 = t1.gethours() + (float)t1.getminutes()/60; 
    float hh2 = t2.gethours() + (float)t2.getminutes()/60; 
    //处理开始结束是同一天的情况 
    if(t1.getday()==t2.getday()){ 
      float tt = 0; 
      tt = hh2 - hh1; 
      if(hh1>=h1&&hh1<=h2&&hh2>=h3){ 
        tt = tt - (h3-h2); 
      } 
      totalhours += tt; 
    }else{ 
      //处理开始结束不是同一天的情况 
      float tt1 = h4 - hh1; 
      float tt2 = hh2 - h1; 
      if(hh1<=h2){ 
        tt1 = tt1 - (h3-h2); 
      } 
      if(hh2>=h3){ 
        tt2 = tt2 - (h3-h2); 
      } 
      totalhours += (tt1 + tt2); 
    } 
    return totalhours; 
  } 
 
  /** 
   * 格式化输出时间: yyyy-mm-dd hh:mm:ss 星期x 
   * @param t 
   * @return 
   */ 
  private string printdate(date t) { 
    string str; 
    string xingqi = null; 
    switch (t.getday()) { 
    case 0: 
      xingqi = "星期天"; 
      break; 
    case 1: 
      xingqi = "星期一"; 
      break; 
    case 2: 
      xingqi = "星期二"; 
      break; 
    case 3: 
      xingqi = "星期三"; 
      break; 
    case 4: 
      xingqi = "星期四"; 
      break; 
    case 5: 
      xingqi = "星期五"; 
      break; 
    case 6: 
      xingqi = "星期六"; 
      break; 
    default: 
      break; 
    } 
    str = format.format(t)+" "+xingqi; 
    return str; 
  } 
 
  /** 
   * 对结束时间进行预处理,使其处于工作日内的工作时间段内 
   * @param t 
   * @return 
   */ 
  private date processendtime(date t) { 
    float h = t.gethours() + (float)t.getminutes()/60; 
    //若结束时间晚于下午下班时间,将其设置为下午下班时间 
    if(h>=h4){ 
      t.sethours(peh); 
      t.setminutes(pem); 
    }else { 
      //若结束时间介于中午休息时间,那么设置为上午下班时间 
      if(h>=h2&&h<=h3){ 
        t.sethours(aeh); 
        t.setminutes(aem); 
      }else{ 
        //若结束时间早于上午上班时间,日期向前推一天,并将时间设置为下午下班时间 
        if(t.gethours()<=h1){ 
          t.settime(t.gettime()-milsecperday); 
          t.sethours(peh); 
          t.setminutes(pem); 
        } 
      } 
    } 
    //若结束时间是周末,那么将结束时间向前推移到最近的工作日的下午下班时间 
    if(t.getday()==0){ 
      t.settime(t.gettime()-milsecperday*(t.getday()==6?1:2)); 
      t.sethours(peh); 
      t.setminutes(pem); 
    } 
    if(t.getday()==6){ 
      t.settime(t.gettime()-milsecperday*(t.getday()==6?1:2)); 
      t.sethours(peh); 
      t.setminutes(pem); 
    } 
    return t; 
  } 
  /** 
   * 对开始时间进行预处理 
   * @param t1 
   * @return 
   */ 
  private date processbegintime(date t) { 
    float h = t.gethours() + (float)t.getminutes()/60; 
    //若开始时间晚于下午下班时间,将开始时间向后推一天 
    if(h>=h4){ 
      t.settime(t.gettime()+milsecperday); 
      t.sethours(abh); 
      t.setminutes(abm); 
    }else { 
      //若开始时间介于中午休息时间,那么设置为下午上班时间 
      if(h>=h2&&h<=h3){ 
        t.sethours(pbh); 
        t.setminutes(pbm); 
      }else{ 
        //若开始时间早于上午上班时间,将hour设置为上午上班时间 
        if(t.gethours()<=h1){ 
          t.sethours(abh); 
          t.setminutes(abm); 
        } 
      } 
    } 
    //若开始时间是周末,那么将开始时间向后推移到最近的工作日的上午上班时间 
    if(t.getday()==0){ 
      t.settime(t.gettime()+milsecperday*(t.getday()==6?2:1)); 
      t.sethours(abh); 
      t.setminutes(abm); 
    }if(t.getday()==6){ 
      t.settime(t.gettime()+milsecperday*(t.getday()==6?2:1)); 
      t.sethours(abh); 
      t.setminutes(abm); 
    } 
    return t; 
  } 
 
  /** 
   * 将字符串形式的时间转换成date形式的时间 
   * @param time 
   * @return 
   */ 
  private date stringtodate(string time){ 
    try { 
      return format.parse(time); 
    } catch (parseexception e) { 
      e.printstacktrace(); 
      return null; 
    } 
  } 
  /** 
   * 去除周末节假日工作小时 
   * @param begintime 
   * @param endtime 
   * @return 
   * @throws parseexception 
   */ 
  public static float calculatehour(string begintime,string endtime) throws parseexception{ 
    calculatehours ch = new calculatehours(); 
    float a=ch.calculatehours(begintime, endtime);  
    calendar startday = calendar.getinstance(); 
     calendar endday = calendar.getinstance(); 
     startday.settime(formatter.parse(begintime)); 
     endday.settime(formatter.parse(endtime)); 
    string[] workday=printday(startday, endday); 
    string[] holiday = new string[]{"01-01","01-02","01-03","05-01","05-02","05-03","10-01","10-02", 
        "10-03","10-04","10-05","10-06","02-08","02-09","02-10"}; 
    calendar now = calendar.getinstance();  
    int year=now.get(calendar.year); //获取当前年份 
    list<string> list = new arraylist<string>(); 
    for (string string : holiday) { 
      string=year+"-"+string; 
      list.add(string); 
    } 
    string[] arr = list.toarray(new string[0]); 
    int holidays = arrcontrast(workday, arr);  
    int holidayhous=holidays*8; 
    float b  = (float)(math.round(a*10))/10; 
    float workhours=b-holidayhous; 
    return workhours; 
  } 
  public static void main(string[] args) throws parseexception { 
    string begintime = "2018-6-1 9:00:00";  
    string endtime = "2018-6-4 10:10:00";  
    calculatehours ch = new calculatehours(); 
    float b=ch.calculatehours(begintime, endtime); 
    system.out.println(b); 
    float a=calculatehours.calculatehour(begintime, endtime); 
    system.out.println(a); 
  } 
  /** 
   * 去除数组中相同日期 
   * @param arr1 
   * @param arr2 
   * @return 
   */ 
  private static int arrcontrast(string[] arr1, string[] arr2){ 
    int count=0; 
    list<string> list = new linkedlist<string>(); 
    for (string str : arr1) { //处理第一个数组,list里面的值为1,2,3,4 
      if (!list.contains(str)) { 
      list.add(str); 
      } 
    } 
    for (string str : arr2) { //如果第二个数组存在和第一个数组相同的值,就删除 
      if(list.contains(str)){ 
      list.remove(str); 
      ++count; 
      } 
    } 
    return count; 
  } 
  private static final dateformat formatter = new simpledateformat("yyyy-mm-dd"); 
  private static string[] printday(calendar startday, calendar endday) { 
     list<string> list = new arraylist<string>(); 
     // 给出的日期开始日比终了日大则不执行打印 
     if (startday.compareto(endday) >= 0) { 
      return new string[]{}; 
     } 
   // 现在打印中的日期 
   calendar currentprintday = startday; 
   while (true) { 
    // 日期加一 
    currentprintday.add(calendar.date, 1); 
    // 日期加一后判断是否达到终了日,达到则终止打印 
    if (currentprintday.compareto(endday) == 0) { 
    break; 
    } 
    list.add(formatter.format(currentprintday.gettime())); 
   } 
    string[] arr = list.toarray(new string[0]); 
    return arr; 
   } 
} 

main方法中的执行结果为:

java计算工作时间除去节假日以及双休日

代码中都有注释,可自行根据需要进行调节。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: