1. 程式人生 > >java 獲取某時間段內每月/每年的最後一天的集合

java 獲取某時間段內每月/每年的最後一天的集合

java 獲取某時間段內每個月的最後一天的集合,當月的獲取當前系統時間的前一天時間。
	private static final String SDF_YMD_H = "yyyy-MM-dd HH:00:00";
	private static final String SDF_YMD = "yyyy-MM-dd 00:00:00";
	private static final String SDF_YM = "yyyy-MM";
	private static final String SDF_Y = "yyyy";


/**
	 * <p>
	 * 獲取時間段內每個月的最後一天,當月的獲取當前系統時間的前一天時間
	 * </P>
	 * 
	 * @param startTime:
	 * @param endTime:
	 * @return List<String>
	 * @author: benbenxion
	 * @date: 2018年3月15日上午10:38:26
	 */
	public static List<String> getLastDayMonthList(String startTime, String endTime) {

		Calendar calendar = Calendar.getInstance();
		// 宣告日期格式:YYYY-MM:2018-02
		SimpleDateFormat sdf = new SimpleDateFormat(SDF_YM);
		// 獲取開始時間 格式:2018-02
		String monthStartTime = startTime.substring(0, 7);
		// 獲取結束時間 格式:2018-03
		String monthEndTime = endTime.substring(0, 7);

		// 獲取當前整月
		String monthDate = getCurMonthDateStr();

		// 儲存每月最後一天日期結果集合
		List<String> dateList = new ArrayList<>();
		// 儲存某時間段中的各個月份
		List<String> monthDateList = new ArrayList<>();

		// 1.判斷startTime是否為當月時間
		if (monthStartTime.equals(monthDate)) {
			// 宣告日期格式:YYYY-MM-DD : 2018-02-14
			SimpleDateFormat format = new SimpleDateFormat(SDF_YMD);

			try {
				// 2.是:獲取當前時間的前一天時間,並放入集合;
				String yesterdayStr = DateTimeUtil.addDay(endTime, -1).substring(0, 11) + "00:00:00";
				
				Date yesterdayDate = format.parse(yesterdayStr);
				dateList.add(format.format(yesterdayDate));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}

		// 3.否:獲取startTime當月以後每個月的最後一天時間,一直到當前月上個月末時間;
		if (!monthStartTime.equals(monthDate)) {
			try {
				// 設定開始時間
				calendar.setTime(sdf.parse(monthStartTime));

				// 把開始時間新增到List集合中
				monthDateList.add(monthStartTime);

				while (true) {
					// 根據日曆的規則,為給定的日曆欄位新增或減去指定的時間量
					calendar.add(Calendar.MONTH, 1);
					// 測試此日期是否在指定日期之後
					if (sdf.parse(monthEndTime).after(calendar.getTime())) {
						monthDateList.add(sdf.format(calendar.getTime()));
					} else {
						break;
					}
				}

			} catch (ParseException e) {
				e.printStackTrace();
			}

			if (ObjectUtils.isNullList(monthDateList)) {
				return dateList;
			}

			// 1.獲取每月最後一天的集合,遍歷月的集合,獲取每個月的最後一天時間
			dateList = getLastDayMonthList(monthDateList, endTime);
		}

		return dateList;
	}

	/**
	 * <p>根據月份集合獲取每個月的最後一天時間</P>
	 * @param list:月份集合
	 * @param endTime:結束時間
	 * @return List<String>
	 * @author: 笨笨熊咦
	 * @date: 2018年3月15日下午1:57:06
	 */
	public static List<String> getLastDayMonthList(List<String> list, String endTime) {

		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(SDF_YMD);
		// 儲存每個月月末時間集合
		List<String> dateList = new ArrayList<>();

		for (String date : list) {
			// 擷取年數
			String yearStr = date.substring(0, 4);
			int year = Integer.parseInt(yearStr);

			String monthStr = date.substring(5, 7);
			int month = Integer.parseInt(monthStr);

			// 設定年月
			calendar.set(Calendar.YEAR, year);
			calendar.set(Calendar.MONTH, month);
			calendar.set(Calendar.DAY_OF_MONTH, 0);
			String lastday = sdf.format(calendar.getTime());
			dateList.add(lastday);
		}

		// 獲取結束時間前一天時間
		try {
			String yesterdayStr = DateTimeUtil.addDay(endTime, -1).substring(0, 11) + "00:00:00";
			Date yesterdayDate = sdf.parse(yesterdayStr);
			dateList.add(sdf.format(yesterdayDate));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateList;
	}

2.獲取某時間段內每年的最後一天,以及結束日期的前一天日期集合。

	/**
	 * <p>獲取某時間段內每年的最後一天,以及結束日期的前一天日期集合</P>
	 * <pre>
	 * 獲取每年最後一天日期,以及結束日期的前一天日期的集合
	 * </pre>
	 * @param startTime:開始時間
	 * @param endTime:結束時間
	 * @return List<String>
	 * @author: 笨笨熊咦  
	 * @date: 2018年3月15日下午2:17:18
	 */
	public static List<String> getLastDayYearList(String startTime, String endTime) {

		Calendar calendar = Calendar.getInstance();
		// 宣告日期格式:YYYY-MM:2018-02
		SimpleDateFormat sdf = new SimpleDateFormat(SDF_Y);
		// 獲取開始時間 格式:2018-02
		String yearStartTime = startTime.substring(0, 4);
		// 獲取結束時間 格式:2018-03
		String yearEndTime = endTime.substring(0, 4);

		// 獲取當前整月
		String yearStr = getCurMonthDateStr();
		String year = yearStr.substring(0, 4);

		// 儲存每月最後一天日期結果集合
		List<String> dateList = new ArrayList<>();
		// 儲存某時間段中的各個月份
		List<String> monthDateList = new ArrayList<>();

		// 1.判斷startTime是否為當月時間
		if (yearStartTime.equals(year)) {
			// 宣告日期格式:YYYY-MM-DD : 2018-02-14
			SimpleDateFormat format = new SimpleDateFormat(SDF_YMD);

			try {
				// 2.是:獲取當前時間的前一天時間,並放入集合;
				String yesterdayStr = DateTimeUtil.addDay(endTime, -1).substring(0, 11) + "00:00:00";
				
				Date yesterdayDate = format.parse(yesterdayStr);
				dateList.add(format.format(yesterdayDate));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}

		// 3.否:獲取startTime當月以後每個月的最後一天時間,一直到當前月上個月末時間;
		if (!yearStartTime.equals(year)) {
			try {
				// 設定開始時間
				calendar.setTime(sdf.parse(yearStartTime));

				// 把開始時間新增到List集合中
				monthDateList.add(yearStartTime);

				while (true) {
					// 根據日曆的規則,為給定的日曆欄位新增或減去指定的時間量
					calendar.add(Calendar.YEAR, 1);
					// 測試此日期是否在指定日期之後
					if (sdf.parse(yearEndTime).after(calendar.getTime())) {
						monthDateList.add(sdf.format(calendar.getTime()));
					} else {
						break;
					}
				}

			} catch (ParseException e) {
				e.printStackTrace();
			}

			if (ObjectUtils.isNullList(monthDateList)) {
				return dateList;
			}

			// 1.獲取每月最後一天的集合,遍歷月的集合,獲取每個月的最後一天時間
			dateList = getLastDayYearList(monthDateList, endTime);
		}

		return dateList;
	}

	
	public static List<String> getLastDayYearList(List<String> list, String endTime) {

		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(SDF_YMD);
		// 儲存每個月月末時間集合
		List<String> dateList = new ArrayList<>();

		for (String date : list) {
			// 擷取年數
			String yearStr = date.substring(0, 4);
			int year = Integer.parseInt(yearStr);

			// 設定年月
			calendar.set(Calendar.YEAR, year);
			calendar.set(Calendar.MONTH, 12);
			calendar.set(Calendar.DAY_OF_MONTH, 0);
			String lastday = sdf.format(calendar.getTime());
			dateList.add(lastday);
		}

		// 獲取結束時間前一天時間
		try {
			String yesterdayStr = DateTimeUtil.addDay(endTime, -1).substring(0, 11) + "00:00:00";
			Date yesterdayDate = sdf.parse(yesterdayStr);
			dateList.add(sdf.format(yesterdayDate));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateList;
	}