この記事では、参考のために休日や週末を含めない勤務時間のJava計算の特定のコードを共有しています。特定のコンテンツは次のとおりです
パッケージcommon.util; java.text.dateformatをインポートします。 java.text.parseexceptionをインポートします。 java.text.simpledateformatをインポートします。 java.util.arraylistをインポートします。 java.util.calendarをインポートします。 Import Java.util.date; java.util.linkedListをインポートします。 java.util.listをインポートします。 public class CalculateHours {SimpleDateFormat Format = new SimpledateFormat( "yyyy-mm-dd hh:mm:ss"); //ここの形式は自分で設定できます//労働時間を設定する:実際の状況に従って時間を調整できますint abh = 9; //朝の労働時間、時間int abm = 00; //朝の労働時間、朝の労働時間、時間int aem = 0; //朝労働時間、微小int pbh = 13;/noring int pbm = 13; 18; //ノーニング時間、時間int pem = 0; // norning労働時間、微小int pem = 0; // norning労働時間、微小フロートh1 = abh+(float)abm/60; float H2 = aeh+(float)aem/60;フロートH3 = PBH+(FLOAT)PBM/60; Float H4 = PEH+(FLOAT)PEM/60;フロートhoursperday = h2-h1+(h4-h3); // 1日あたりの労働時間int daysperweek = 5; //長年の労働日数milsecperday = 1000*60*60*24; // float hourat hoursperweek = hoursperday*dayperweek;入力文字列時間日T1 = stringtodate(begintime); // real start time date t2 = stringtodate(endtime); // real endtime // preprocess the t1 = processbegintime(t1); T2 = ProcessEndTime(T2); //開始時間が終了時間より遅い場合、0(t1.gettime()> t2.gettime()){return 0; } //開始時刻から終了までの完全な数週間int weekcount =(int)((t2.gettime()-t1.gettime())/(milsecperday*7));フロートTotalhours = 0; Totalhours += weekcount * hoursperweek; //終了時間を調整して、開始時間と終了時間が1週間の期間内になるように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; //プロセスの開始と終了は同じ日です(t1.getDay()== t2.getDay()){float tt = 0; TT = HH2 -HH1; if(hh1> = h1 && hh1 <= h2 && hh2> = h3){tt = tt-(h3 -h2); } totalhours += tt; } else {//プロセスの開始と終了は同じ日ではありません。フロートTT2 = HH2 -H1; if(hh1 <= h2){tt1 = tt1-(h3 -h2); } if(hh2> = h3){tt2 = tt2-(h3 -h2); } totalhours + =(tt1 + tt2); } totalhoursを返します。 } / ** *フォーマット出力時間:yyyy-mm-dd hh:mm:ss week x * @param t * @return * / private string printdate(date t){string str;文字列xingqi = null; switch(t.getday()){case 0:xingqi = "Sunday";壊す;ケース1:XingQi = "Monday";壊す;ケース2:xingqi = "火曜日";壊す;ケース3:xingqi = "水曜日";壊す;ケース4:xingqi = "木曜日";壊す;ケース5:xingqi = "金曜日";壊す;ケース6:xingqi = "土曜日";壊す;デフォルト:break; } str = format.format(t)+""+xingqi; strを返します。 }/** *終了時間を前処理して、営業日内に作業期間内にあるように * @param t * @return */private date processendtime(date t){float h = t.gethours() +(float)t.getminutes()/60; //終了時間が午後の営業時間外の場合は、(h> = h4){t.sethours(peh); t.setminutes(pem); } else {//終了時間が午後の営業時間外の場合、(h> = h2 && h <= h3){t.sethours(aeh); T.SetMinutes(AEM); } else {//終了時間が午前中の時間より早い場合、1日前に日付を前進させ、時間を営業時間外に設定します(t.gethours()<= h1){t.settime(t.gettime() - milsecperday); T.sethours(peh); t.setminutes(pem); }}} //終了時間が週末の場合、終了時間は最近の営業日の午後の午後に転送されます(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); } 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; //開始時間が午後のオフゲット作業時間より遅い場合、1つの日の開始時間を1日戻します(h> = h4){t.settime(t.gettime()+milsecperday); T.sethours(abh); T.SetMinutes(ABM); } else {//開始時間が昼休みの間にある場合、(h> = h2 && h <= h3){t.sethours(pbh); T.SetMinutes(PBM); } else {//開始時間が朝の労働時間より早い場合、時間を朝の労働時間を設定するif(t.gethours()<= h1){t.sethours(abh); T.SetMinutes(ABM); }}} //開始時間が週末の場合、開始時間を直近の営業日の朝の労働時間に戻します(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; } / ** *文字列の形式の時間を日付の形式に変換する時間 * @param time * @return * / private date stringtodate(string time){try {return format.parse(time); } catch(parseexception e){e.printstacktrace(); 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);カレンダーlartday = charend.getInstance();カレンダーendday = calendar.getInstance(); startday.settime(formatter.parse(begintime)); endday.settime(formatter.parse(endtime)); string [] workday = printday(lastday、endday); string [] holidy = 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); //現在の年リスト<string> list = new ArrayList <String>()を取得します。 for(string string:holidy){string = year+" - "+string; list.add(string); } string [] arr = list.toarray(new String [0]); int holidays = arrcontrast(workday、arr); int holidyhous = 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); } / ** * arrayで同じ日付を削除 * @param arr1 * @param arr2 * @return * / private static int arrcontrast(string [] arr1、string [] arr2){int count = 0;リスト<文字列>リスト= new LinkedList <String>(); for(string str:arr1){//最初の配列の処理、リストの値は1、2、3、4です。 }} for(string str:arr2){// 2番目の配列が最初の配列と同じ値を持っている場合、if(list.contains(str)){list.remove(str); ++カウント; }} return count; } private static final dateformat formatter = new SimpledateFormat( "yyyy-mm-dd"); private static string [] printday(Calendarの初日、カレンダー終了日){list <string> list = new ArrayList <String>(); //指定された日付の開始日が終了日よりも大きい場合、(startday.compareto(endday)> = 0){return new String [] {}; } //今すぐ印刷された日付カレンダーcurrentPrintday = lastday; while(true){// currentPrintday.add(calendar.date、1)に1つの日付を追加します。 //終了日に到達するかどうかを判断する日付を1つ追加した後、それを印刷すると終了します(currentPrintDay.comPareto(endday)== 0){break; } list.add(formatter.format(currentPrintday.getTime())); } string [] arr = list.toarray(new String [0]); arrを返します。 }}主な方法の実行結果は次のとおりです。
コードにはコメントがあり、必要に応じて調整できます。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。