PackageCom.wdcloud.monitoring.common;ImportJava.text.SimpleDateFormat;Importjava.util.ArrayList;ImportJava.util.Calendar;Importjava.util.Date;Importjava.util.List;ImportCom.wdcloud.monitoring.Model.WeekInfoModel; Public classDateutils {//whether to print the log Private Static BooleanIslog =false; /*** Get information about this month's week and date period * *@return */ Public StaticList<weekinfomodel>getthismonthweekdate () {SimpleDateFormat format=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); List<WeekInfoModel> list =NewArraylist<weekinfomodel>(); //1 Get the first day of the monthDate Firstdayofmonth =Getfirstdayofmonth (); Calendar firstdayofmonthcal=calendar.getinstance (); Firstdayofmonthcal.setfirstdayofweek (Calendar.monday); Firstdayofmonthcal.settime (Firstdayofmonth); //2 get first day of the first week of this monthDate Firstweekfirstday =Getfirstdayoffirstweekofmonth (); Calendar firstweekfirstdaycal=calendar.getinstance (); Firstweekfirstdaycal.setfirstdayofweek (Calendar.monday); Firstweekfirstdaycal.settime (Firstweekfirstday); //3 Get the start and end time of this cycleDate sdate =NULL;//First day of the first week of this monthDate eDate =NULL;//Last day of the last week of this monthCalendar SCal =calendar.getinstance (); Calendar ecal=calendar.getinstance (); Scal.setfirstdayofweek (Calendar.monday); Scal.setfirstdayofweek (Calendar.monday); //get the first day of the month is the week intWeekDay = Firstdayofmonthcal.get (calendar.day_of_week)-1; //If this is the weekend, the start time is the first day of the first week of this month//Otherwise the starting time is the first day of the month, 7 days backwards . if(WeekDay = = 6 | | weekDay = = 0 | | weekDay = = 1) {//Saturday or SundaySdate =Firstweekfirstday; } Else{firstweekfirstdaycal.gettime (); Firstweekfirstdaycal.add (Calendar.date,-7); Sdate=Firstweekfirstdaycal.gettime (); } scal.settime (Sdate); //end time is start time + 34 days (5 weeks)Ecal.settime (sdate); EDate=Ecal.gettime (); Ecal.add (Calendar.date,34); EDate=Ecal.gettime (); Date cDate=NewDate (); if(Cdate.gettime () <Edate.gettime ()) {EDate=cDate; } if(Islog) {System.out.println ("This month the statistics start and end time is:" + format.format (sdate) + "to" +Format.format (eDate)); System.out. println ("-------------------------------------------------------------------------------"); } //4 cycle to get weekly information for(inti = 0; I < 5; i++) {//Cycle 5 weeksWeekinfomodel model =NewWeekinfomodel (); Calendar EndTime=calendar.getinstance (); Endtime.setfirstdayofweek (Calendar.monday); Endtime.settime (sdate); Endtime.set (Calendar.hour_of_day,23); Endtime.set (Calendar.minute,59); Endtime.set (Calendar.second,59); Endtime.add (Calendar.date,6); Model.setindexofweek (i+ 1); Model.setstartdateofweek (sdate); Model.setenddateofweek (Endtime.gettime ()); Date currdate=NewDate (); //If the current time is within a week if(Sdate.gettime () <currdate.gettime ()&& endtime.gettime (). GetTime () >Currdate.gettime ()) {Model.setenddateofweek (currdate); if(Islog) {System.out.println ("This month" + (i + 1) + "Week start and end time is:" + format.format (sdate) + "to" +Format.format (currdate)); } list.add (model); Break; } if(Islog) {System.out.println ("This month" + (i + 1) + "Week start and end time is:" + format.format (sdate) + "to" +Format.format (Endtime.gettime ())); } list.add (model); Scal.add (Calendar.date,7); Sdate=Scal.gettime (); } returnlist; } //get the first day of the month Public StaticDate Getfirstdayofmonth () {Calendar now=calendar.getinstance ();//Date date = new Date (2015-1900, 12-1, 1);//now.settime (date);Now.set (Calendar.date, Now.getactualminimum (calendar.date)); Now.set (Calendar.hour_of_day,0); Now.set (Calendar.minute,0); Now.set (Calendar.second,0); returnNow.gettime (); } //get first day of the first week of this month Public StaticDate Getfirstdayoffirstweekofmonth () {Calendar now=calendar.getinstance (); Now.setfirstdayofweek (Calendar.monday); Now.settime (Getfirstdayofmonth ()); inti = 1; while(Now.get (calendar.day_of_week)! =calendar.monday) {now.set (calendar.day_of_month, I++);//set the date of the Week 1 for this month} Date firstmonday= Now.gettime ();//Date and time of acquisitionString Dtstr =NewSimpleDateFormat ("Yyyy-mm-dd"). Format (firstmonday);//formatted Date if(islog) {System.out. println ("-------------------------------------------------------------------------------"); System.out.println ("The first day of the first week of this month is:" +dtstr); System.out. println ("-------------------------------------------------------------------------------"); } returnNow.gettime (); } /*** Get weekly and datetime information for this week * *@return */ Public StaticWeekinfomodel getthisweekdate () {Weekinfomodel model=NewWeekinfomodel (); SimpleDateFormat DF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); //According to today's time to get this week is part of the week of this monthCalendar now =calendar.getinstance (); Now.set (Calendar.day_of_week, calendar.monday); //get the date of this Monday intWOM =Now.get (Calendar.week_of_month); Model.setindexofweek (WOM); //get this week's start time based on today's timeNow.set (calendar.hour_of_day, 0); Now.set (Calendar.minute,0); Now.set (Calendar.second,0); Now.set (Calendar.millisecond,0); System.out.println ("********" + now.get (calendar.year) + "Year" + ((Now.get (calendar.month)) + 1) + "Month" + Wom + "Week Monday date is:" +Df.format (Now.gettime ())); Model.setstartdateofweek (Now.gettime ()); //get this week's end time based on today's timeNow.set (Calendar.day_of_week, calendar.sunday); Now.add (Calendar.week_of_year,1); Now.set (Calendar.hour_of_day,23); Now.set (Calendar.minute,59); Now.set (Calendar.second,59); Now.set (Calendar.millisecond,999); System.out.println ("********" + now.get (calendar.year) + "Year" + ((Now.get (calendar.month)) + 1) + "Month" + Wom + "Week Sunday date is:" +Df.format (Now.gettime ())); Model.setenddateofweek (Now.gettime ()); returnmodel; }}
Get information about this month's week and date time period