设置一个月的开始和结束

1.以前的办法

/**
	 * 根据月份设置活动的开始何结束时间
	 * 
	 * @param activity
	 * @param activityMonthPara
	 *            客户端选择的月份
	 * @return
	 */
	public Advertisement setActivtityDate(Advertisement activity,
			String activityMonthPara) {
		int year;
		String month = null;
		String day = "01";
		Calendar today = Calendar.getInstance();
		today.setTime(new Date());
		year = getActivityYear(today, activityMonthPara);

		// today.add(Calendar.MONTH, 1);
		// today.add(Calendar.SECOND, -1);

		if (activityMonthPara.equals("12")) {
			// 活动开始时间
			month = activityMonthPara;
			String startDateStr = year + "/" + month + "/" + day;
			activity.setStartDate(DateUtil.getInstance().parseStringToDate(
					startDateStr));

			// 活动结束时间
			year += 1;
			month = "01";
			String endDateStr = year + "/" + month + "/" + day;
			activity.setEndDate(DateUtil.getInstance().parseStringToDate(
					endDateStr));
		} else {
			int intMonth = Integer.parseInt(activityMonthPara);
			if (intMonth < 9) {
				// 其它月份活动开始时间
				month = activityMonthPara;
				String startDateStr = year + "/0" + month + "/" + day;
				activity.setStartDate(DateUtil.getInstance().parseStringToDate(
						startDateStr));

				// 其它月份活动结束时间,结束月份要加1
				month = String.valueOf(Integer.parseInt(activityMonthPara) + 1);
				String endDateStr = year + "/0" + month + "/" + day;
				activity.setEndDate(DateUtil.getInstance().parseStringToDate(
						endDateStr));
			} else if (intMonth == 9) {
				// 其它月份活动开始时间
				month = activityMonthPara;
				String startDateStr = year + "/0" + month + "/" + day;
				activity.setStartDate(DateUtil.getInstance().parseStringToDate(
						startDateStr));

				// 其它月份活动结束时间,结束月份要加1
				month = String.valueOf(Integer.parseInt(activityMonthPara) + 1);
				String endDateStr = year + "/" + month + "/" + day;
				activity.setEndDate(DateUtil.getInstance().parseStringToDate(
						endDateStr));
			} else {
				// 其它月份活动开始时间
				month = activityMonthPara;
				String startDateStr = year + "/" + month + "/" + day;
				activity.setStartDate(DateUtil.getInstance().parseStringToDate(
						startDateStr));

				// 其它月份活动结束时间,结束月份要加1
				month = String.valueOf(Integer.parseInt(activityMonthPara) + 1);
				String endDateStr = year + "/" + month + "/" + day;
				activity.setEndDate(DateUtil.getInstance().parseStringToDate(
						endDateStr));
			}
		}

		return activity;
	}


2.现在的办法

	/**
	 * 根据月份设置活动的开始何结束时间
	 * 
	 * @param activity
	 * @param activityMonthPara
	 *            客户端选择的月份
	 * @return
	 */
	public Advertisement setActivtityDate(Advertisement activity,
			String activityMonthPara) {
		
		Date startDate = DateUtil.getInstance().parseStringToDate(activityMonthPara, "yyyy-MM");
		activity.setStartDate(startDate);

		Calendar today = Calendar.getInstance();
		today.setTime(startDate);
		today.add(Calendar.MONTH, 1);
		today.add(Calendar.SECOND, -1);
		
		activity.setEndDate(today.getTime());

		System.out.println("endDate:" + today.getTime());

		return activity;
	}


3.一个日期的辅助方法


	/**
	 * 功能:将dateString字符串转换成dateModul型的日期格式
	 * 
	 * @param dateString
	 * @param dateModul
	 * @return
	 */
	public Date parseStringToDate(String dateString, String dateModul) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(dateModul);
		Date tempDate = null;
		try {
			tempDate = dateFormat.parse(dateString);
		} catch (ParseException e) {
			System.out.println("日期格式转化错误");
			e.printStackTrace();
		}
		return tempDate;
	}









.

你可能感兴趣的:(java,Calender)