Start of work day in Generation interval (Monday)-End (Friday) Date pair method

Source: Internet
Author: User
/** * Generate workday Start (Monday)-End (Friday) Date pair method * For example: * Input: Parameter start_time = "2011-05-04" (Wednesday), end_time= "2011-07-14" (Thursday) * return: [* 
	 ["2011-05-04", "2011-05-06"], * ["2011-05-09", "2011-05-13"], * ..., * ["2011-07-4", "2011-07-14"] *] * Description: Returns the list of the first element in the array, the 1th value is the start date, the 2nd value is one weeks after the start date, the last element in the 5 * list, the 1th value is one weeks 1 before the end date, and the 2nd value is the end date * The middle part of the list each dollar In the group of primes, the 1th value is the Week 1, the 2nd value is the Week 5 * @param start_time * @param end_time * @return * * Private list<string[]> Gettimewee
		Klypairlist (String start_time,string end_time) {list<string[]> List = new arraylist<string[]> ();
		SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
		Date start_date = null;
		
		Date end_date = null;
			try {start_date = Sdf.parse (start_time);
			end_date = Sdf.parse (end_time); int dayOfWeek1 = Caculateweekdays (start_date);
			1-> Sunday,2-> Monday int dayOfWeek2 = caculateweekdays (end_date); int pre_days = 8-DAYOFWEEK1; i...saturday int last_days = DAYOFWEEK2; Sunday...i Long diff_days = Caculatedays (start_date,end_date);
			int remain_weeks = (int) (diff_days+2-pre_days-(last_days+1))/7;
			
			Calculates the first start, end pair Addpair (list,start_date,null,pre_days-2);
				Todo add for (int i=0; i<remain_weeks; i++) {Date start_d = caculatedate (start_date, pre_days+i*7+1);
			Addpair (list,start_d,null,4);
		//Calculate the last start, end pair Addpair (list,null,end_date,-(last_days-2));
		catch (ParseException e) {e.printstacktrace ();
	} return list; } private void Addpair (list<string[]> List, date start_date, date end_date, int days) {SimpleDateFormat SDF =
		New SimpleDateFormat ("Yyyy-mm-dd");
		
		Calendar C = calendar.getinstance ();
		string[] Start_end_pair = new string[2];
			if (start_date!=null && end_date==null) {c.settime (start_date);
			C.add (Calendar.day_of_month, days);
			Date d = c.gettime ();
			Start_end_pair[0] = Sdf.format (start_date);
		START_END_PAIR[1] = Sdf.format (d); }else if (Start_date==null &Amp;& end_date!=null) {c.settime (end_date);
			C.add (Calendar.day_of_month, days);
			Date d = c.gettime ();
			Start_end_pair[0] = Sdf.format (d);
		START_END_PAIR[1] = Sdf.format (end_date);
	} list.add (Start_end_pair);
		Public date caculatedate (date start_date, int days) {Calendar c = calendar.getinstance ();
		C.settime (start_date);
		C.add (Calendar.day_of_month, days);
	return C.gettime ();
		private int caculateweekdays (date date) {Calendar c = calendar.getinstance ();
		C.settime (date);
	Return C.get (Calendar.day_of_week); private int Caculatedays (date start_date, date end_date) {return Math.Round (End_date.gettime ()/(1000*60*60*24)-star
	T_date.gettime ()/(1000*60*60*24)); }

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.