# Calculate the number of working days between any two dates in Java

Source: Internet
Author: User

Main ideas:

For any 2 dates, such as: Date_start=2006-10-1, date_end=2006-10-14, first calculate the interval between the dates (days), and then get their next Monday date for Date_start and Date_end respectively. So you can get a new full date interval that divides 7 (this new date interval has removed the week's question), in other words, we can get the number of weeks between these two new dates, and multiply this week by 5, which is the working date (tmpworkingdays). But this date is not the date we want, and the next thing we need to do is calculate the offset of the date_start,date_end between these dates for the time offset between the new dates that are produced by them, Date_start (Date_start_ Change) is required, and the Date_end offset (date_end_change) is subtracted. Finally, we just need to use Tmpworkingdays+date_start_change-date_end_change is the actual working day we require. The following are all implementation codes (two dates are not a problem in the year).

` package com.date.utlit; import Java.text.SimpleDateFormat; import Java.util.Calendar; import java.util.Date; import Java.util.GregorianCalendar; /** * Calculates the working day within any 2 dates (without regard to national holidays) * @author User * */ public class Datecal { /** * @param args 　*/ public static void Main (string[] args) { try { String Strdatestart = "2006-10-1"; String strdateend = "2006-10-14"; SimpleDateFormat sdf = new SimpleDateFormat ("Yyyy-mm-dd"); Date Date_start = Sdf.parse (Strdatestart); Date date_end = Sdf.parse (strdateend); datecal app = new datecal (); Calendar Cal_start = Calendar.getinstance (); Calendar cal_end = Calendar.getinstance (); Cal_start.settime (Date_start); Cal_end.settime (date_end); System.out.println ("Week--&gt;" + app.getchineseweek (Cal_start) + "Date--&gt;" + cal_start.get (calendar.year) + "-" + (Cal_start.get (calendar.month) + 1) + "-" + cal_start.get (calendar.day_of_month)); System.out.println ("Week--&gt;" + app.getchineseweek (cal_end) + "Date--&gt;" + cal_end.get (calendar.year) + "-" + (Cal_end.get (calendar.month) + 1) + "-" + cal_end.get (calendar.day_of_month)); System.out.println ("Weekday is--&gt;" + app.getworkingday (Cal_start, cal_end)); System.out.println ("Rest day--&gt;" +app.getholidays (Cal_start, cal_end)); } catch (Exception e) {//Todo:handle exception 　} } public int Getdaysbetween (Java.util.Calendar d1, Java.util.Calendar D2) { if (D1.after (D2)) {//swap dates so that D1 are start and D2 is-end Java.util.Calendar swap = D1; D1 = d2; d2 = swap; 　} int days = D2.get (Java.util.Calendar.DAY_OF_YEAR) -D1.get (Java.util.Calendar.DAY_OF_YEAR); int y2 = d2.get (Java.util.Calendar.YEAR); if (D1.get (Java.util.Calendar.YEAR)!= y2) { D1 = (Java.util.Calendar) d1.clone (); do { days + + d1.getactualmaximum (Java.util.Calendar.DAY_OF_YEAR); D1.add (Java.util.Calendar.YEAR, 1); } while (D1.get (Java.util.Calendar.YEAR)!= y2); 　} return days; } /** * Calculates the number of days between 2 dates * @param d1 * @param D2 * @return 　*/ public int Getworkingday (Java.util.Calendar d1, Java.util.Calendar D2) { int result =-1; if (D1.after (D2)) {//swap dates so that D1 are start and D2 is-end Java.util.Calendar swap = D1; D1 = d2; d2 = swap; 　} int betweendays = Getdaysbetween (d1, D2); int charge_date = 0; int charge_start_date = Date offset of 0;//start date int charge_end_date = date offset of 0;//end date //date is not in the same date int stmp; int etmp; stmp = 7-d1.get (Calendar.day_of_week); etmp = 7-d2.get (Calendar.day_of_week); if (stmp!= 0 &amp;&amp; stmp!= 6) {//start date is Saturday and Sunday with an offset of 0 charge_start_date = stmp-1; 　 } if (etmp!= 0 &amp;&amp; etmp!= 6) {//End date is Saturday and Sunday offset is 0 charge_end_date = etmp-1; 　 } //　} result = (Getdaysbetween (this.getnextmonday (D1), This.getnextmonday (D2))/7) * 5 + charge_start_date-charge_end_date; //system.out.println ("charge_start_date&gt;" + charge_start_date); //system.out.println ("charge_end_date&gt;" + charge_end_date); System.out.println ("Between day is--&gt;" + betweendays); return result; } public String Getchineseweek (Calendar date) { final String daynames[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; int dayofweek = Date.get (Calendar.day_of_week); //System.out.println (daynames[dayofweek-1]); return daynames[dayofweek-1]; } /** * Get date of the next Monday date 　* * @param date * @return 　*/ Public Calendar getnextmonday (calendar date) { Calendar result = null; result = date; do { result = (Calendar) result.clone (); Result.add (calendar.date, 1); } while (Result.get (Calendar.day_of_week)!= 2); return result; } /** 　* * @param d1 * @param D2 * @return 　*/ public int getholidays (Calendar D1,calendar D2) { return This.getdaysbetween (D1, D2)-this.getworkingday (D1, D2); } } `

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.