Java time period coincidence Time Difference

Source: Internet
Author: User
// It Can Be Used for attendance and holidays, lunch breaks, and night shifts to calculate the Picking Time
Package test; import Java. text. parseposition; import Java. text. simpledateformat; import Java. util. date; public class test {/*** there are two time periods (b1 ----- E1) [B2 ----- E2] and four time points. * It is equivalent to two line segments (B represents the start point, e-generation table endpoint, B <= E), 4 endpoints. ** There are three possible cases: * 1. contains (two) * (b1 --- [B2 ----- E2] -- E1) if (b1 <= b2 & E1> = e2) * [B2 --- (b1 ----- E1) -- E2: If (b1> = b2 & E1 <= e2) ** 2. intersection (two) * (b1 --- [B2 --- E1) ---- E2] If (b1 <B2 & E1 <E2 & E1> B2) * [B2 --- (b1 --- E2] ---- E1) if (b1> B2 & B1 <E2 & E2 <E1) ** 3.*(b1 ----- E1) [B2 ----- E2] or [B2 ----- E2] (b1 ----- E1) if (E1 <B2 | B1> E2) at this time, the coincidence time is zero. * @ Param begintime1 * @ Param endtime1 * @ Param begintime2 * @ Param endtime2 * @ return */public static string gettimecoincidence (date begintime1, date endtime1, date begintime2, date endtime2) {long b1 = begintime1.gettime (); long e1 = endtime1.gettime (); long b2 = begintime2.gettime (); long e2 = endtime2.gettime (); string coincidencetime; if (b1 <= b2 & E1> = e2) {// (b1 --- [B2 ----- E2] -- E1) 1 contains 2system. out. println ("1 Contains 2 "); coincidencetime = gettimedifference (endtime2, begintime2);} else if (b1> = b2 & E1 <= e2) {// [B2 --- (b1 ----- E1) -- E2] 2 contains 1system. out. println ("2 contains 1"); coincidencetime = gettimedifference (endtime1, begintime1);} else if (b1> = b2 & B1 <= E2 & E2 <= e1) {// (b1 --- [B2 --- E1) ---- E2] Intersection 1system. out. println ("intersection"); coincidencetime = gettimedifference (endtime2, begintime1);} else if (b1 <= b2 & E1 <= E2 & E1> = b2) {// [B2 --- (b1 --- E2] ---- E1) intersection 2sy Stem. out. println ("intersection"); coincidencetime = gettimedifference (endtime1, begintime2);} else if (E1 <= b2 | B1 >=e2) {// (b1 ----- E1) [B2 ----- E2] or [B2 ----- E2] (b1 ----- E1) do not intersection coincidencetime = "0";} else {coincidencetime = ""; system. out. println ("unexpected date combination, cannot calculate coincidence! ");} System. Out. println (" coincidence days: ["+ coincidencetime +. "); Return coincidencetime;}/*** calculate two time differences * @ Param date1 * @ Param date2 * @ return */public static string gettimedifference (date date1, date date2) {stringbuffer df = new stringbuffer (); try {// Computing Unit setting (millisecond 1000 * seconds 60 * minute 60 * hour 24); long num = (date1.gettime () -date2.gettime ()/(1000*60); If (Num> = 0) DF. append (Num);} catch (exception e) {e. printstacktrace ();} return DF. tostring ();}/*** String Conversion time format * @ Param date * @ return */public static date stringtodate (string date) {If (date = NULL) {return NULL;} simpledateformat format = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); parseposition Pos = new parseposition (0); Date strtodate = format. parse (date, POS); Return strtodate;} public static void main (string [] ARGs) {date begintime1 = stringtodate ("22:00:00 "); date endtime1 = stringtodate ("08:30:00"); Date begintime2 = stringtodate ("00:00:00"); Date endtime2 = stringtodate ("02:30:00"); system. out. println (gettimecoincidence (begintime1, endtime1, begintime2, endtime2 ));}}

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.