This article shares the specific code for java calculation of working hours, not including holidays and weekends, for your reference. The specific content is as follows
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"); //The format here can be set by yourself//Set working hours: the time can be adjusted according to the actual situation int abh = 9;//Morning working hours, hour int abm = 00;//Morning working hours, minute int aeh = 12;//Morning working hours, hour int aem = 0;//Morning working hours, minute int pbh = 13;//Norning working hours, hour int pbm = 00;//Norning working hours, minute int peh = 18;//Norning working hours, hour int pem = 0;//Norning working hours, minute int pem = 0;//Norning working hours, minute 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);//Number of working hours per day int daysPerWeek = 5;//Number of working days per week long milsecPerDay = 1000*60*60*24;//Number of milliseconds per day float hoursPerWeek = hoursPerDay*daysPerWeek;//Number of hours per week public float calculateHours(String beginTime, String endTime){ //Convert the input string time Date t1 = stringToDate(beginTime);//Real start time Date t2 = stringToDate(endTime);//Real end time//Preprocess the time t1 = processBeginTime(t1); t2 = processEndTime(t2); //If the start time is later than the end time, return 0 if(t1.getTime()>t2.getTime()){ return 0; } //The full number of weeks from the start time to the end time int weekCount = (int) ((t2.getTime()-t1.getTime())/(milsecPerDay*7)); float totalHours = 0; totalHours += weekCount * hoursPerWeek; //Adjust the end time so that the start time and end time are within a one-week period t2.setTime(t2.getTime()-weekCount*7*milsecPerDay); int dayCounts = 0;//Record the working day between the start time and the end time//Adjust the start time so that the start time and end time are on the same day, or within adjacent working days. 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; //The process start and end is the same day 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{ //The process start and end is not the same day 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; } /** * Format output time: yyyy-mm-dd hh:mm:ss Week x * @param t * @return */ private String printDate(Date t) { String str; String xingqi = null; switch (t.getDay()) { case 0: xingqi = "Sunday"; break; case 1: xingqi = "Monday"; break; case 2: xingqi = "Tuesday"; break; case 3: xingqi = "Wednesday"; break; case 4: xingqi = "Thursday"; break; case 5: xingqi = "Friday"; break; case 6: xingqi = "Saturday"; break; default: break; } str = format.format(t)+" "+xingqi; return str; } /** * Preprocess the end time so that it is within the working period within the working day* @param t * @return */ private Date processEndTime(Date t) { float h = t.getHours() + (float)t.getMinutes()/60; //If the end time is later than the afternoon off-hour, set it to the afternoon off-hour if(h>=h4){ t.setHours(peh); t.setMinutes(pem); }else { //If the end time is between the afternoon off-hour, set it to the morning off-hour if(h>=h2&&h<=h3){ t.setHours(aeh); t.setMinutes(aem); }else{ //If the end time is earlier than the morning off-hour, push the date forward one day, and set the time to the afternoon off-hour if(t.getHours()<=h1){ t.setTime(t.getTime()-milsecPerDay); t.setHours(peh); t.setMinutes(pem); } } } //If the end time is a weekend, then the end time will be forwarded to the afternoon after-hours of the most recent working day 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); } if(t.getDay()==6){ t.setTime(t.getTime()-milsecPerDay*(t.getDay()==6?1:2)); t.setHours(peh); t.setMinutes(pem); } return t; } /** * Preprocessing the start time* @param t1 * @return */ private Date processBeginTime(Date t) { float h = t.getHours() + (float)t.getMinutes()/60; //If the start time is later than the afternoon off-get off work time, push the start time back one day if(h>=h4){ t.setTime(t.getTime()+milsecPerDay); t.setHours(abh); t.setMinutes(abm); }else { //If the start time is between the lunch break, then set to afternoon working time if(h>=h2&&h<=h3){ t.setHours(pbh); t.setMinutes(pbm); }else{ //If the start time is earlier than morning working time, set hour to morning working time if(t.getHours()<=h1){ t.setHours(abh); t.setMinutes(abm); } } } //If the start time is a weekend, then move the start time back to the morning working time of the most recent working day 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; } /** * Time to convert the time in the form of a string to the form of a Date* @param time * @return */ private Date stringToDate(String time){ try { return format.parse(time); } catch (ParseException e) { e.printStackTrace(); return null; } } /** * Remove weekends and holidays working hours* @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); //Get the current 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); } /** * Remove the same date in the array* @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) { //Processing the first array, the values in list are 1, 2, 3, 4 if (!list.contains(str)) { list.add(str); } } for (String str : arr2) { //If the second array has the same value as the first array, delete 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 the given date start date is larger than the end date, print will not be performed if (startDay.compareTo(endDay) >= 0) { return new String[]{}; } // The date printed now Calendar currentPrintDay = startDay; while (true) { // Add one date to currentPrintDay.add(Calendar.DATE, 1); // After adding one date to determine whether the end date is reached, print it will be terminated if (currentPrintDay.compareTo(endDay) == 0) { break; } list.add(FORMATTER.format(currentPrintDay.getTime())); } String[] arr = list.toArray(new String[0]); return arr; } }The execution result in the main method is:
There are comments in the code and can be adjusted as needed.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.