[Original tool] Java background calculates the working day time difference between two dates

Source: Internet
Author: User

[Code implementation]: the time difference between working days between two dates (accurate to milliseconds)

 

[Code features ]:

 

1. Supports cross-year support.

 

2. Get the Time Difference Accurate to milliseconds.

 

3. National statutory holidays are not included (for example, only the list method is added =. = ).

 

Package COM. wednesday. tools. timetools; </P> <p> Import Java. text. parseexception; <br/> Import Java. text. simpledateformat; <br/> Import Java. util. calendar; <br/> Import Java. util. date; </P> <p> public class getworkdaytimemillisecond {</P> <p>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> getworkdaytimemillisecond A = new getworkdayti Memillisecond (); <br/> long B =. getworkdaytimeinmillis ("2010-05-17 8-00-00", "2010-05-18 9-00-00", "yyyy-mm-dd hh-mm-SS"); <br/> system. out. println (B); <br/>}< br/>/** <br/> * Get the workday time within two periods (only remove the weekend time between two dates, not Removed on statutory holidays) <br/> * @ Param start-start time. There are three overload methods available. Long, long, and date type <br/> * @ Param end-end time. There are three overload methods. You can input long, long, date type <br/> * @ return long type time difference object <br/> */<br/> Public long getworkdaytimeinm Illis (long start, long end) {<br/> // If the start time is later than the end time, switch the two <br/> If (Start> end) {<br/> long temp = start; <br/> Start = end; <br/> end = temp; <br/>}< br/> // obtain the calendar type object of the start time and end time based on the parameters. <br/> calendar sdate = calendar. getinstance (); <br/> calendar edate = calendar. getinstance (); <br/> sdate. settimeinmillis (start); <br/> edate. settimeinmillis (end); <br/> // if the two dates are in the same week and are not weekend dates, the time difference is returned directly, increasing execution efficiency <br/> If (sdate. get (calendar. Year) = edate. get (calendar. year) <br/> & sdate. get (calendar. week_of_year) = edate. get (calendar. week_of_year) <br/> & sdate. get (calendar. day_of_week )! = 1 & sdate. Get (calendar. day_of_week )! = 7 <br/> & edate. Get (calendar. day_of_week )! = 1 & edate. Get (calendar. day_of_week )! = 7) {<br/> return New Long (end-Start ); <br/>}< br/> // obtain the start date and end date of the next Monday. <br/> calendar snextm = getnextmonday (sdate ); <br/> calendar enextm = getnextmonday (edate); <br/> // obtain the actual number of days between the two Mondays <br/> int days = getdaysbetween (snextm, enextm ); <br/> // obtain the number of working days between the two Mondays (the number of days between the two Mondays is certainly 7, and the number of working days accounts for 5/7 of the total) <br/> int workdays = days/7*5; <br/> // get the Start Time Offset <br/> long scharge = 0; <br/> If (sdate. get (calendar. day_of_week )! = 1 & sdate. Get (calendar. day_of_week )! = 7) {<br/> // The offset is calculated only when the start time is not a weekend. <br/> scharge + = (7-sdate.get (calendar. day_of_week) * 24*3600000; <br/> scharge-= sdate. get (calendar. hour_of_day) * 3600000; <br/> scharge-= sdate. get (calendar. minute) * 60000; <br/> scharge-= sdate. get (calendar. second) * 1000; <br/> scharge-= sdate. get (calendar. millisecond); <br/>}< br/> // get the offset of the end time <br/> long echarge = 0; <br/> If (edate. get (calendar. day_of_week )! = 1 & edate. Get (calendar. day_of_week )! = 7) {<br/> // The offset is calculated only when the end time is not a weekend. <br/> echarge + = (7-edate.get (calendar. day_of_week) * 24*3600000; <br/> echarge-= edate. get (calendar. hour_of_day) * 3600000; <br/> echarge-= edate. get (calendar. minute) * 60000; <br/> echarge-= edate. get (calendar. second) * 1000; <br/> echarge-= edate. get (calendar. millisecond); <br/>}< br/> // calculate the final result, specifically, the Time offset of the start time plus the workdays, offset of the end time minus <br/> return workdays * 24*3600000 + scharge- Echarge; <br/>}< br/> Public long getworkdaytimeinmillis (long start, long end) {<br/> return getworkdaytimeinmillis (start. longvalue (), end. longvalue (); <br/>}< br/> Public long getworkdaytimeinmillis (date start, date end) {<br/> return getworkdaytimeinmillis (start. gettime (), end. gettime (); <br/>}< br/> Public long getworkdaytimeinmillis (string start, string end, string format) {<br/> simpledateformat SDF = new simpledateformat (format); <br/> date sdate; <br/> date edate; <br/> try {<br/> sdate = SDF. parse (start); <br/> edate = SDF. parse (end); <br/> return getworkdaytimeinmillis (sdate, edate); <br/>} catch (parseexception e) {<br/> E. printstacktrace (); <br/> return New Long (0); <br/>}< br/> private calendar getnextmonday (calendar Cal) {<br/> int addnum = 9-cal.get (calendar. day_of_week); <br/> If (AD Dnum = 8) addnum = 1; // Sunday condition <br/> Cal. add (calendar. date, addnum); <br/> return Cal; <br/>}< br/>/** <br/> * obtain the actual number of days between two dates, supports cross-year <br/> */<br/> Public int getdaysbetween (calendar start, calendar end) {<br/> If (start. after (end) {<br/> calendar swap = start; <br/> Start = end; <br/> end = swap; <br/>}< br/> int days = end. get (calendar. day_of_year)-start. get (calendar. day_of_year); <br/> int y2 = end. get (calendar. Year); <br/> If (start. Get (calendar. Year )! = Y2) {<br/> Start = (calendar) Start. clone (); <br/> do {<br/> days + = start. getactualmaximum (calendar. day_of_year); <br/> Start. add (calendar. year, 1); <br/>}while (start. get (calendar. year )! = Y2); <br/>}< br/> return days; <br/>}</P> <p >}< br/>

Contact Us

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.