根据指定日期 获取过去七天的起始日期 获取过去三周 每个周的起始 获取过去半年 每个月的起始日期

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zxf1242652895/article/details/91039428
 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个月的起始日期

 

 

展开阅读全文

没有更多推荐了,返回首页