根據指定日期 獲取過去七天的起始日期 獲取過去三週 每個周的起始 獲取過去半年 每個月的起始日期

 private static String datestimes;//頁面傳過來的日期值 如:2019-06-06 12:12:12
@Override
	public List<Object> getAllMachOEEHistory(String t0, String t1, String type) {
		ArrayList<String> pastTimeList=new ArrayList<>();//過去七天
		ArrayList<String> pastWeekList=new ArrayList<>();//過去三個月
		ArrayList<String>pastMonthList=new ArrayList<>();//過去六個月
		
		List<Object> resultReturnList=new ArrayList<>();
		List<Float> dataList=new ArrayList<>();
 		List<Object>detailsobject=new ArrayList<>();
		List<Object> detailList=new ArrayList<>();
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		getAllMachOEEDetailReturnEntity returnEntity=new getAllMachOEEDetailReturnEntity();
		Calendar calendar=Calendar.getInstance();
		datestimes=t0;
		try {
			Date date=format.parse(t0);
			if(type.equals("day")){
				for (int i = 6; i >= 0; i--) {
					 pastTimeList.add(getPastDate(i,date));
		        }
				for (int i = 0; i < pastTimeList.size(); i++) {
					float result=0f;
 					String start=pastTimeList.get(i)+" 00:00:00";
					String end=pastTimeList.get(i)+" 23:59:59";
					float[] datas = newSearchUtil.getPeriodOfOEE(start, end,null, "factory", format, calendar);
					//detailsobject=getAllMachOEEDetail2(start, end);
 					result=datas[3];
					dataList.add(result);
					//detailList.add(detailsobject);
				}
				returnEntity.setLabel("OEE");
 				returnEntity.setData(dataList);//oee 值
 				//returnEntity.setDetailData(detailList);
 			 
				resultReturnList.add(returnEntity);
	 			resultReturnList.add(pastTimeList);
 				//清空操作
	 			pastTimeList=null;
			}else if(type.equals("week")){
 				pastWeekList.add(getThisWeekTimeInterval());
				pastWeekList.add(getlastWeekOne());
				pastWeekList.add(getlastWeekThree());
				pastWeekList.add(getlastWeekThree());
				float result=0f;
				for(int i=0;i<pastWeekList.size();i++){
					String timesall=pastWeekList.get(i);
					String start=timesall.substring(0, 10);
					start=start+" 00:00:00";
					String end=timesall.substring(11,timesall.length());
					end=end+" 23:59:59";
					float[] datas = newSearchUtil.getPeriodOfOEE(start, end,null, "factory", format, calendar);
					result=datas[3];
					dataList.add(result);
				}
				returnEntity.setLabel("OEE");
 				returnEntity.setData(dataList);//oee 值
  				resultReturnList.add(returnEntity);
	 			resultReturnList.add(pastWeekList);
 				//清空操作
	 			pastWeekList=null;
			}else{//根據月份選擇
   		 		for(int i = 5; i >= 0; i--){
   		 		  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 		 			Calendar c1=Calendar.getInstance();
 		 			 datestimes=datestimes.substring(0,19);//去掉時分秒
 		 	        Date datese = sdf.parse(datestimes);
 		 	        c1.setTime(datese);
 		 			c1.add(Calendar.MONTH,-i );
 					int lastMonthMaxDay=c1.getActualMaximum(Calendar.DAY_OF_MONTH);
 					c1.set(c1.get(Calendar.YEAR), c1.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59);
					String monthLastDay = sdf.format(c1.getTime()); //上月第一天
					SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-01");
 					String monthFirstDay = sdf2.format(c1.getTime()); //上月最後一天
					String monthday=monthFirstDay+"-"+monthLastDay;
					pastMonthList.add(monthday);
 		 		}
   		     	float result=0f;
   		 		for(int i =0;i<pastMonthList.size();i++){
	   		 		String timesall=pastMonthList.get(i);
					String start=timesall.substring(0, 10);
					start=start+" 00:00:00";
					String end=timesall.substring(11,timesall.length());
					end=end+" 23:59:59";
					float[] datas = newSearchUtil.getPeriodOfOEE(start, end,null, "factory", format, calendar);
					result=datas[3];
					dataList.add(result);
   		 		}
   		 	    returnEntity.setLabel("OEE");
				returnEntity.setData(dataList);//oee 值
				resultReturnList.add(returnEntity);
 		     	resultReturnList.add(pastMonthList);
				//清空操作
 		     	pastMonthList=null;
			}
 			 
		} catch (ParseException e) {
 			e.printStackTrace();
 			return null;
		}finally{
			dataList=null;
		}
 		return resultReturnList;
	}
	/**
     * 獲取過去第幾天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past,Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - past);
        Date today = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String result = sdf.format(today);
        return result;
    }
    
    /**
     * 獲取起止日期
     * @param sdf 需要顯示的日期格式
     * @param date 需要參照的日期
     * @param n 最近n周
     * @param option 0 開始日期;1 結束日期
     * @param k 0 包含本週 1 不包含本週
     * @return
     * @throws ParseException 
     */
    public static String getFromToDate(int n, int option, int k) throws ParseException {
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        datestimes=datestimes.substring(0,19);//去掉時分秒
        Date date = sdf.parse(datestimes);
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        int offset = 0 == option ? 1 - dayOfWeek : 7 - dayOfWeek;
        int amount = 0 == option ? offset - (n - 1  + k) * 7 : offset - k * 7;
        calendar.add(Calendar.DATE, amount);
        return sdf.format(calendar.getTime());
    }
    //當週
    public static String getNWeekTimeInterval(int n) throws ParseException {
        String beginDate = getFromToDate(n, 0, 0);
        String endDate = getFromToDate(n, 1, 0);
         
        return beginDate + "-" + endDate+" ";
    }
 	//上週
    public static String getNWeekTimeIntervalOne(int n) throws ParseException {
        String beginDate = getFromToDate(n, 0, 1);
        String endDate = getFromToDate(n, 1, 1);
        
        return beginDate + "-" + endDate+" ";
    }
    //上上週
    public static String getNWeekTimeIntervalTwo(int n) throws ParseException {
        String beginDate = getFromToDate(n, 0, 2);
        String endDate = getFromToDate(n, 1, 2);
        
        return beginDate + "-" + endDate+" ";
    }
    //上上上週
    public static String getNWeekTimeIntervalThree(int n) throws ParseException {
    	String beginDate = getFromToDate(n, 0, 3);
    	String endDate = getFromToDate(n, 1, 3);
    	 
         return beginDate + "-" + endDate+" ";
    }
    
    public static String getThisWeekTimeInterval() {
        try {
			return getNWeekTimeInterval(1);
		} catch (ParseException e) {
 			e.printStackTrace();
		}
		return null;
    }
    public static String getlastWeekOne() {
        try {
			return getNWeekTimeIntervalOne(1);
		} catch (ParseException e) {
 			e.printStackTrace();
		}
		return null;
    }
    public static String getlastWeekTwo() {
        try {
			return getNWeekTimeIntervalTwo(1);
		} catch (ParseException e) {
 			e.printStackTrace();
		}
		return null;
    }
    public static String getlastWeekThree() {
        try {
			return getNWeekTimeIntervalThree(1);
		} catch (ParseException e) {
 			e.printStackTrace();
		}
		return null;
    }
   

需求::根據指定某天的數據 獲取該天以及過去6天的數據  根據該天 獲取該月以及過去三個月的起始日期 根據該天 獲取該月以及過去5個月的起始日期

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章