日期区间

	/**
	 * 算日期的
	 * @param type
	 * @return 相应区间段的起止日期
	 */
	private static String[] getDateDiff(String type){
		
		String[] ret = new String[2];
		
		SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		
		//周日算起,周三返回4
		int day_of_week = c.get(c.DAY_OF_WEEK);
		//正常算法,3号返回3
		int day_of_month = c.get(c.DAY_OF_MONTH);
		//从0算起,12月返回11
		int month_of_year = c.get(c.MONTH);
		//返回正常
		int year = c.get(c.YEAR);

		
		System.out.println(day_of_week+","+day_of_month+","+month_of_year+","+year);
		
		if("week".equals(type)){
			c.set(c.DATE, day_of_month-day_of_week-5);
			ret[0] = sm.format(c.getTime());
			c.add(c.DATE, 7);
			ret[1] = sm.format(c.getTime());
		}
		
		if("month".equals(type)){
			c.add(c.MONTH, -1);
			c.set(c.DATE, 1);
			ret[0] = sm.format(c.getTime());
			c.add(c.MONTH, 1);
			ret[1] = sm.format(c.getTime());
		}
		if("season".equals(type)){
			int s_diff = month_of_year%3;
			c.add(c.MONTH, s_diff==0?-3:-s_diff-3);
			c.set(c.DATE, 1);
			ret[0] = sm.format(c.getTime());
			c.add(c.MONTH, 3);
			ret[1] = sm.format(c.getTime());
		}
		if("year".equals(type)){
			c.set(c.YEAR, year-1);
			c.set(c.MONTH, 0);
			c.set(c.DATE, 1);
			ret[0] = sm.format(c.getTime());
			c.add(c.MONTH, 12);
			ret[1] = sm.format(c.getTime());
		}
		
		return ret;
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值