JAVA date A time period of a total of several weeks, each day of the week

Source: Internet
Author: User

/*** Query The total number of weeks of a time period and the current time of the query is the week *@paramStart *@paramEnd *@return     */    Private int[] Selectweeknum (Date start,date end,intWeekintflag) {Java.util.Calendar now=java.util.Calendar.getInstance (); Java.util.Calendar C_total=java.util.Calendar.getInstance (); Java.util.Calendar C_begin=java.util.Calendar.getInstance (); Java.util.Calendar C_end=java.util.Calendar.getInstance (); intCount = 0; intWeektotal = 0; Try{c_begin.settime (start);            C_end.settime (end); Now.settime (NewSimpleDateFormat ("Yyyy-mm-dd"). Parse (Sdf.format (NewDate ()))); intBegin =C_begin.get (Java.util.Calendar.WEEK_OF_YEAR); intover =C_end.get (Java.util.Calendar.WEEK_OF_YEAR); intz =Now.get (Java.util.Calendar.WEEK_OF_YEAR); if(C_begin.getweekyear ()! =c_end.getweekyear ()) {Date Totalyear=NewSimpleDateFormat ("Yyyy-mm-dd"). Parse (C_begin.getweekyear () + "12-31");                C_total.settime (totalyear); intx =C_total.get (Java.util.Calendar.WEEK_OF_YEAR);  while(x==1) {C_total.add (Java.util.Calendar.DAY_OF_MONTH,-1); X=C_total.get (Java.util.Calendar.WEEK_OF_YEAR); }                //If the end time is exactly Sunday, don't add 1 .                intWeekNum = This. Dayforweek (NewSimpleDateFormat ("Yyyy-mm-dd"). Format (end); intDuoyu = 0; if(weeknum==7) {Duoyu= X-begin+1; }Else{Duoyu= X-begin + +; } weektotal= over +Duoyu; } Else {                //If the end time is exactly Sunday, don't add 1 .                intWeekNum = This. Dayforweek (NewSimpleDateFormat ("Yyyy-mm-dd"). Format (end); if(weeknum==7) {Weektotal= Over-begin; }Else{weektotal= Over-begin + 1; }            }            if(C_begin.getweekyear ()! =now.getweekyear ()) {                //calculates the total number of weeks                intx =C_total.get (Java.util.Calendar.WEEK_OF_YEAR);  while(x==1) {C_total.add (Java.util.Calendar.DAY_OF_MONTH,-1); X=C_total.get (Java.util.Calendar.WEEK_OF_YEAR); }                intWeekNum = This. Dayforweek (NewSimpleDateFormat ("Yyyy-mm-dd"). Format (start); intDuoyu =0; if(weeknum==7) {Duoyu= X-begin + +; }Else{Duoyu= X-begin + 1; } Count= Duoyu +Z; } Else {                intWeekNum = This. Dayforweek (NewSimpleDateFormat ("Yyyy-mm-dd"). Format (start); if(weeknum==7) {Count= (Z-begin + 1) +1; }Else{Count= (Z-begin + 1); }            }            if(C_end.gettime (). GetTime () < Now.gettime (). GetTime () | | Count < 0) {                //If the current time is greater than the semester end time, the first week is displayed silentlyCount = 1; }
Drop-down box The default display of things can be deleted or retained according to the actual situationif(Week! = 0 && Flag = = 1) {Count=Week; } } Catch(Exception e) {e.printstacktrace (); } return New int[]{count,weektotal}; }
/*** Determine the current date is the day of the week * *@paramPtime The time to Judge@returnDayforweek Judgment Result * @Exception exception occurred*/     Public intDayforweek (String ptime)throwsException {simpledateformat format=NewSimpleDateFormat ("Yyyy-mm-dd"); Java.util. Calendar C=java.util. calendar.getinstance ();        C.settime (Format.parse (ptime)); intDayforweek = 0; if(C.get (Java.util. Calendar.day_of_week) = = 1) {Dayforweek= 7; }Else{Dayforweek= C.get (java.util. Calendar.day_of_week)-1; }        returnDayforweek; }
/**      * Query the     start and end times of the week @param  date     @return     *    / Private map<string,date>  onceweek (date date) {       new  GregorianCalendar ();        Currentdate.settime (date);
Currentdate.setfirstdayofweek (Java.util.Calendar.MONDAY); Currentdate.set (Java.util.Calendar.HOUR_OF_DAY, 0);        Currentdate.set (Java.util.Calendar.MINUTE, 0);        Currentdate.set (Java.util.Calendar.SECOND, 0);        Currentdate.set (Java.util.Calendar.DAY_OF_WEEK, Java.util.Calendar.MONDAY);        Date Dbegin = (date) Currentdate.gettime (). Clone (); Currentdate.setfirstdayofweek (Java.util.Calendar.MONDAY); Currentdate.set (Java.util.Calendar.HOUR_OF_DAY),currentdate.set (Java.util.Calendar.MINUTE,;); Currentdate.set (Java.util.Calendar.SECOND),currentdate.set (Java.util.Calendar.DAY_OF_WEEK, Java.util.Calendar.SUNDAY); Date DEnd =(date) Currentdate.gettime (). Clone ();//Return Sundays and one weeks of dates and weeks map<string,date> Map = new hashmap< > (); Map.put ("Dbegin", Dbegin); Map.put ("DEnd", DEnd); return map;      
/*** Check the time of week *@paramStart *@paramEnd *@paramWeek *@paramFlag *@return     */    Privatelist< list<schedulevo> > Onceweeklist (date Start, Date end,intWeekintflag) {List< list<schedulevo>> List =NewArraylist<>(); Try {            int[] Total = This. Selectweeknum (Start, end,week,flag); intWeektotal = total[1]; Java.util.Calendar currentdate=NewGregorianCalendar ();            Currentdate.settime (start);  for(inti = 0; i < weektotal; i++) {Map<String,Date> map = This. Onceweek (Currentdate.gettime ()); List<ScheduleVO> lDate = finddates (Map.get ("Dbegin"), Map.get ("DEnd")); Currentdate.settime (Map.get ("DEnd")); Currentdate.add (Java.util.Calendar.DAY_OF_MONTH,1);            List.add (lDate); }        }Catch(Exception e) {System.out.print (E.getmessage ()); }        returnlist; }
  /*** Check the current date and the date specified is the day of the week *@paramBegindate *@paramEndDate *@return     */     Public StaticList<schedulevo>finddates (date begindate, date endDate) {//Digital to ChineseSimpleDateFormat sif =NewSimpleDateFormat ("Yyyy-mm-dd"); Map<Integer,String> weeknumstring =NewHashmap<>(); Weeknumstring.put (1, "Monday"); Weeknumstring.put (2, "Tuesday"); Weeknumstring.put (3, "Wednesday"); Weeknumstring.put (4, "Thursday"); Weeknumstring.put (5, "Friday"); Weeknumstring.put (6, "Saturday"); Weeknumstring.put (7, "Sunday"); Schedulevo Schedule1=NewSchedulevo (); List<ScheduleVO> lDate =NewArraylist<>();        Schedule1.setdate (Sif.format (begindate)); Schedule1.setweekname ("Monday"); Schedule1.setweek (1);        Ldate.add (SCHEDULE1); Java.util.Calendar Cal=java.util.Calendar.getInstance ();        Cal.settime (begindate); BooleanBcontinue =true; intWeek = 1;  while(bcontinue) {//adds or subtracts a specified amount of time for a given calendar field according to the calendar's rulesCal.add (Java.util.Calendar.DAY_OF_MONTH, 1); //test whether this date is after the specified date            if(Enddate.after (Cal.gettime ())) {Week++; Schedulevo Schedule=NewSchedulevo ();                Schedule.setdate (Sif.format (Cal.gettime ()));                Schedule.setweekname (Weeknumstring.get (week));                Schedule.setweek (week);            Ldate.add (schedule); }Else{bcontinue=false; }        }        returnlDate; }


JAVA date A time period of a total of several weeks, each day of the week

Related Article

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.