------------------------ 题记
由于项目需求,在网上又没查到,于是自己写了些计算日期的方法,然后share to you。
------------------------ 代码
/**
* 计算今天的起始日期
* @param now 当前时间
* @return 今日起始日期
* @throws Exception
*/
@SuppressWarnings("deprecation")
private Date figureOutTodayFirstTime(Date now) throws Exception {
Calendar cld = Calendar.getInstance();
cld.clear();
cld.set(Calendar.YEAR, now.getYear() + 1900);
cld.set(Calendar.MONTH, now.getMonth());
cld.set(Calendar.DATE, now.getDate());
Date todayFirst = cld.getTime();//今日起始日期
return todayFirst;
}
/**
* 计算某一年整年时间范围的工具方法
* @param from 起始日期
* @param thru 终止日期
* @param year 现在时间
* @return 时间范围数据传输对象
* @throws Exception
*/
private FromAndThruDateUtil figureOutDateWholeYearUtil(int year) throws Exception {
FromAndThruDateUtil andThruDateDto = new FromAndThruDateUtil();
//该年第一天
Calendar calendarLast = Calendar.getInstance();
calendarLast.clear();
calendarLast.set(Calendar.YEAR, year + 1900);
calendarLast.roll(Calendar.DAY_OF_YEAR, -1);
Date currYearLast = calendarLast.getTime();
//该年最后一天
Calendar calendarFirst = Calendar.getInstance();
calendarFirst.clear();
calendarFirst.set(Calendar.YEAR, year + 1900);
Date currYearFirst = calendarFirst.getTime();
//赋值
andThruDateDto.setFrom(currYearFirst);
andThruDateDto.setThru(currYearLast);
return andThruDateDto;
}
/**
* 计算本周时间
* @param now 当前时间
* @return
* @throws Exception
*/
@SuppressWarnings("deprecation")
private FromAndThruDateUtil figureOutDateThisWeekUtil(Date now) throws Exception {
FromAndThruDateUtil fromAndThruDateUtil = new FromAndThruDateUtil();
//计算本周起始日期
Calendar cld = Calendar.getInstance();
cld.clear();
cld.set(Calendar.YEAR, now.getYear() + 1900);
cld.set(Calendar.MONTH, now.getMonth());
int decreaseNum = 0;
if (now.getDay() == 1) {//周一
decreaseNum = 0;
} else if (now.getDay() == 2) {//周二
decreaseNum = -1;
} else if (now.getDay() == 3) {//周三
decreaseNum = -2;
} else if (now.getDay() == 4) {//周四
decreaseNum = -3;
} else if (now.getDay() == 5) {//周五
decreaseNum = -4;
} else if (now.getDay() == 6) {//周六
decreaseNum = -5;
} else if (now.getDay() == 0) {//周日
decreaseNum = -6;
}
cld.set(Calendar.DATE, now.getDate() + decreaseNum);
fromAndThruDateUtil.setFrom(cld.getTime());
//计算本周结束日期
fromAndThruDateUtil.setThru(now);
return fromAndThruDateUtil;
}
/**
* 计算日期方法
* @param now 现在时间
* @param time 客户选择时间
* @return 时间范围数据传输对象
* @throws Exception
*/
@SuppressWarnings("deprecation")
private FromAndThruDateUtil figureOutDate(Date now, String time) throws Exception {
FromAndThruDateUtil dateUtil = new FromAndThruDateUtil();
Date from = null;
Date thru = null;
if (time == null || time.equals("")) return dateUtil;
else if (time.equals("0")) {//当天
from = figureOutTodayFirstTime(now);
thru = now;
} else if (time.equals("1")) {//本周
return figureOutDateThisWeekUtil(now);
} else if (time.equals("2")) {//本月
Calendar cld = Calendar.getInstance();
cld.clear();
cld.set(Calendar.YEAR, now.getYear() + 1900);
cld.set(Calendar.MONTH, now.getMonth());
from = cld.getTime();
thru = now;
} else if (time.equals("")) {//最近三个月
//三个月前
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -3);
Date myDate = calendar.getTime();
Calendar theCalendar = Calendar.getInstance();
theCalendar.clear();
theCalendar.set(Calendar.YEAR, myDate.getYear() + 1900);
theCalendar.set(Calendar.MONTH, myDate.getMonth());
Date theDate = theCalendar.getTime();
//赋值
from = theDate;
thru = now;
} else if (time.equals("")) {//今年内
//从今年起
Calendar calendar = Calendar.getInstance();
calendar.set(now.getYear() + 1900, 0, 1, 0, 0, 0);
Date myDate = calendar.getTime();
//赋值
from = myDate;
thru = now;
} else if (time.equals("")) {//2012年
return figureOutDateWholeYearUtil(112);
} else if (time.equals("")) {//2011年
return figureOutDateWholeYearUtil(111);
} else if (time.equals("")) {//2010年
return figureOutDateWholeYearUtil(110);
} else if (time.equals("")) {//2009年
return figureOutDateWholeYearUtil(109);
} else if (time.equals("")) {//2008年
return figureOutDateWholeYearUtil(108);
} else if (time.equals("")) {//2008年以前
//该年最后一天
Calendar calendarFirst = Calendar.getInstance();
calendarFirst.clear();
calendarFirst.set(Calendar.YEAR, 108 + 1900);
Date currYearFirst = calendarFirst.getTime();
//赋值
from = currYearFirst;
thru = now;
}
//赋值
dateUtil.setFrom(from);
dateUtil.setThru(thru);
return dateUtil;
}