java获取季度日期

获取季度第一天和最后一天


	/**
	 * 获取当前季度日期——方式一
	 * @param dateStr 当前日期字符串,默认为当前日期
	 * @return String[]{季度起始日期, 季度结束日期}
	 */
	public static String[] getSeasonDay(String dateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
		Date date;
		if (StringUtils.isNotEmpty(dateStr)) {
			try {
				date = sdf.parse(dateStr);
			} catch (ParseException e) {
				log.error("转换字符串:{}为日期失败,错误信息:{}", dateStr, e);
				return null;
			}
		} else {
			date = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int month = c.get(Calendar.MONTH);
		//数学原理:
		// moth属性=月份-1,3个月=1季度,
		// 那么季度的最大月份÷3 =季度,(月份-1(即month属性))÷3向下取整 = 上一个季度;
		// 上个季度*3=上个季度最大月份=当前季度最小月份-1=当前季度最小月份month属性
		Calendar startC = Calendar.getInstance();
		startC.setTime(date);
		int seasonStartMonthNum = (int) (Math.floor(month / 3.0) * 3);
		startC.set(Calendar.MONTH, seasonStartMonthNum);
		startC.set(Calendar.DAY_OF_MONTH, 1);
		String seasonStartDateStr = sdf.format(startC.getTime());
		Calendar endC = Calendar.getInstance();
		endC.setTime(date);
		//当前季度最大月=当前最小月+2;
		int seasonEndMonthNum;
		seasonEndMonthNum = seasonStartMonthNum + 2;
		endC.set(Calendar.MONTH, seasonEndMonthNum);
		endC.set(Calendar.DAY_OF_MONTH, endC.getActualMaximum(Calendar.DAY_OF_MONTH));
		String seasonEndDateStr = sdf.format(endC.getTime());
		return new String[]{seasonStartDateStr, seasonEndDateStr};
	}

	/**
	 * 获取当前季度日期——方式二
	 * @param dateStr 当前日期字符串,默认为当前日期
	 * @return String[]{季度起始日期, 季度结束日期}
	 */
	 public static String[] getSeasonDay22(String dateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
		Date date;
		if (StringUtils.isNotEmpty(dateStr)) {
			try {
				date = sdf.parse(dateStr);
			} catch (ParseException e) {
				log.error("转换字符串:{}为日期失败,错误信息:{}", dateStr, e);
				return null;
			}
		} else {
			date = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		//数学原理:
		// moth属性=月份-1,3个月=1季度,
		// 那么季度的最大月份÷3 =季度,(月份(即month属性+1))÷3向上取整 = 季度;
		// 当前季度最大月份=季度*3,而最大月month属性=最大月份-1
		int month = c.get(Calendar.MONTH);
		int seasonEndMonthNum = (int) (Math.ceil((month + 1) / 3.0) * 3) - 1;
		Calendar endC = Calendar.getInstance();
		endC.setTime(date);
		endC.set(Calendar.MONTH, seasonEndMonthNum);
		endC.set(Calendar.DAY_OF_MONTH, endC.getActualMaximum(Calendar.DAY_OF_MONTH));
		Calendar startC = Calendar.getInstance();
		startC.setTime(date);
		//季度最小月份= 季度最大月份-2;
		int seasonStartMonthNum = seasonEndMonthNum - 2;
		startC.set(Calendar.MONTH, seasonStartMonthNum);
		startC.set(Calendar.DAY_OF_MONTH, 1);
		String seasonStartDateStr = sdf.format(startC.getTime());
		String seasonEndDateStr = sdf.format(endC.getTime());
		return new String[]{seasonStartDateStr, seasonEndDateStr};
	}
	

获取季度

	/**
	 * 获取当前时间季度
	 * @param dateStr
	 * @return
	 */
	public int getQuarterOfDay(String dateStr) {
		int month = 0;
		try {
			Date date = parseDateStr(dateStr);
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			//由于month从0开始,所有这里+1
			month = c.get(Calendar.MONTH) + 1;
		} catch (Exception e) {
			log.error("转换日期失败,", e);
			return 0;
		}
		return month % 3 == 0 ? month / 3 : month / 3 + 1;
	}

你可能感兴趣的:(java,java,开发语言)