1. 程式人生 > >給定一個時間範圍,以起始時間為頭,以結束時間為尾,按月,季度,年進行分段時間範圍提供

給定一個時間範圍,以起始時間為頭,以結束時間為尾,按月,季度,年進行分段時間範圍提供

 /**
     * 通過開始時間和結束時間查詢中間的每個月的開始和結束時間
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    public static List<Map<String, String>> queryInterval(String startTime, String endTime,int type) throws ParseException {
        /*
            type 1:月 2:季度 3:年
         */
        if(type==1){
            return queryIntervalMonth(startTime, endTime);
        }else if(type==2){
            return queryIntervalQuarter(startTime, endTime);
        }else{
            return queryIntervalYear(startTime, endTime);
        }
    }
/**
     * 通過開始時間和結束時間查詢中間的每個月的開始和結束時間
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
	public static List<Map<String, String>> queryIntervalMonth(String startTime, String endTime) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar bef = Calendar.getInstance();
		Calendar aft = Calendar.getInstance();
		bef.setTime(sdf.parse(startTime));
		aft.setTime(sdf.parse(endTime));
		int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
		int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
		List<Map<String, String>> mapList = Lists.newArrayList();
        int minMonth = Math.abs(month + result);
        Map<String, String> dateMap = Maps.newHashMap();
		if(minMonth>0){
            dateMap.put("start", startTime);
            dateMap.put("end", getMaxMonthDate(startTime) + " 23:59:59");
            mapList.add(dateMap);
            for (int i = 0; i < minMonth; i++) {
                dateMap = Maps.newHashMap();
                Calendar start = Calendar.getInstance();
                start.setTime(sdf.parse(startTime));
                start.add(Calendar.MONTH, 1);
                startTime = date2String(start.getTime(), DateUtil.PATTERN_STANDARD);
                dateMap.put("start", getMinMonthDate(startTime) + " 00:00:00");
                if(i+1 == minMonth){
                    dateMap.put("end", endTime);
                }else{
                    dateMap.put("end", getMaxMonthDate(startTime) + " 23:59:59");
                }
                mapList.add(dateMap);
            }
        }else{
            dateMap.put("start", startTime);
            dateMap.put("end", endTime);
            mapList.add(dateMap);
        }
		return mapList;
	}

	/**
	 * 獲取月份起始日期
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String getMinMonthDate(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		return dateFormat.format(calendar.getTime());
	}

	/**
	 * 獲取月份最後日期
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String getMaxMonthDate(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return dateFormat.format(calendar.getTime());
	}

	/**
	 * 獲取年份起始日期
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String getMinYearDate(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		return dateFormat.format(calendar.getTime());
	}

	/**
	 * 獲取年份最後日期
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String getMaxYearDate(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
        calendar.set(Calendar.DAY_OF_YEAR,1);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
		return dateFormat.format(calendar.getTime());
	}


    /**
     * 通過開始時間和結束時間查詢中間的每個月的開始和結束時間
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    public static List<Map<String, String>> queryInterval(String startTime, String endTime,int type) throws ParseException {
        /*
            type 1:月 2:季度 3:年
         */
        if(type==1){
            return queryIntervalMonth(startTime, endTime);
        }else if(type==2){
            return queryIntervalQuarter(startTime, endTime);
        }else{
            return queryIntervalYear(startTime, endTime);
        }
    }

    /**
     * 通過開始時間和結束時間查詢中間的每個月的開始和結束時間
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    public static List<Map<String, String>> queryInterval(Date startTime, Date endTime,int type) throws ParseException {
        return queryInterval(DateUtil.date2String(startTime,DateUtil.PATTERN_STANDARD),DateUtil.date2String(endTime,DateUtil.PATTERN_STANDARD),type);
    }

    private static List<Map<String,String>> queryIntervalYear(String startTime, String endTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        bef.setTime(sdf.parse(startTime));
        aft.setTime(sdf.parse(endTime));
        int year = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR));
        List<Map<String, String>> mapList = Lists.newArrayList();
        Map<String, String> dateMap = Maps.newHashMap();
        dateMap.put("start", startTime);
        if(year>0){
            dateMap.put("end", getMaxYearDate(startTime) + " 23:59:59");
            mapList.add(dateMap);
            for (int i = 0; i < year; i++) {
                dateMap = Maps.newHashMap();
                bef.add(Calendar.YEAR, 1);
                dateMap.put("start", DateUtil.date2String(getYearFirst(Integer.parseInt(DateUtil.date2String(bef.getTime(),"yyyy"))),DateUtil.PATTERN_STANDARD));
                if (i + 1 == year) {
                    dateMap.put("end", endTime);
                }else{
                    dateMap.put("end", getMaxYearDate(DateUtil.date2String(bef.getTime(),DateUtil.PATTERN_STANDARD))+" 23:59:59");
                }
                mapList.add(dateMap);
            }
        }else{
            dateMap.put("end", endTime);
            mapList.add(dateMap);
        }
        return mapList;
    }

    private static List<Map<String,String>> queryIntervalQuarter(String startTime, String endTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        bef.setTime(sdf.parse(startTime));
        aft.setTime(sdf.parse(endTime));
        int thisMonth = bef.get(Calendar.MONTH)+1; //當前月
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
        List<Map<String, String>> mapList = Lists.newArrayList();
        Map<String, String> dateMap = Maps.newHashMap();
        dateMap.put("start",startTime);
        int minMonth = Math.abs(month + result);
        if(thisMonth == 1 || thisMonth==4 || thisMonth==7 || thisMonth==10){
            bef.add(Calendar.MONTH, 2);
            minMonth-=2;
        }else if(thisMonth == 2 || thisMonth==5 || thisMonth==8 || thisMonth==11){
            bef.add(Calendar.MONTH, 1);
            minMonth-=1;
        }
        dateMap.put("end",getMaxMonthDate(date2String(bef.getTime(), DateUtil.PATTERN_STANDARD))+" 23:59:59");
        mapList.add(dateMap);
        bef.add(Calendar.MONTH, 1);
        startTime = getMinMonthDate(date2String(bef.getTime(), DateUtil.PATTERN_STANDARD))+" 00:00:00";
        while (minMonth > 0) {
            dateMap = Maps.newHashMap();
            dateMap.put("start",startTime);
            Calendar end = Calendar.getInstance();
            end.setTime(sdf.parse(startTime));
            end.add(Calendar.MONTH, 2);
            minMonth -= 3;
            if(minMonth>0){
                dateMap.put("end",getMaxMonthDate(date2String(end.getTime(), DateUtil.PATTERN_STANDARD))+" 23:59:59");
            }else{
                dateMap.put("end",endTime);
            }
            mapList.add(dateMap);
            bef.add(Calendar.MONTH, 3);
            startTime = getMinMonthDate(date2String(bef.getTime(), DateUtil.PATTERN_STANDARD))+" 00:00:00";
        }
        return mapList;
    }

	public static Date getCurrYearFirst(){
		Calendar currCal=Calendar.getInstance();
		int currentYear = currCal.get(Calendar.YEAR);
		return getYearFirst(currentYear);
	}

	/**
	 * 獲取某年第一天日期
	 * @param year 年份
	 * @return Date
	 */
	public static Date getYearFirst(int year){
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		Date currYearFirst = calendar.getTime();
		return currYearFirst;
	}


執行結果如上,改下type就好- -