1. 程式人生 > >java時間減少一週,一月,一年例項

java時間減少一週,一月,一年例項

1、例項程式碼:

		String hql=null;
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String beginDate=null;
		String endDate=format.format(Calendar.getInstance().getTime());
		if ("week".equalsIgnoreCase(findTime)) {
			cal.add(Calendar.WEEK_OF_MONTH, -1);
		}
		else if("month".equalsIgnoreCase(findTime)){
			cal.add(Calendar.MONTH, -1);
		}
		else if ("year".equalsIgnoreCase(findTime)) {
			cal.add(Calendar.YEAR, -1);
		}
		beginDate=format.format(cal.getTime());
		hql="from GwtNews where date between '"+beginDate+"' and '"+endDate+"'"
		    +" and title like '%"+title+"%' order by date desc";
		System.out.println("begin="+beginDate+" "+endDate);



2、在“year”的情況下輸出為:

begin=2015-08-28 2016-08-28