@Java实现获取一个时间的开始和结束时间求上一个时间的开始和结束
#Java实现
我是第一次的写这个东西,那我就随意一点好了。大家不要介意!
1、工具类的两个方法(按照类型获取 年月周日的第一天)
①按照类型获取 年月周日的第一天
/**
* 获取日期的年月周日的第一天
*
* @param type 所求的类型(年月周日)
* @param date
* @return
*/
public static Date beginDayByDate(int type, Date date)
{
Date firstDate = null;
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
switch (type)
{
case TYPE_YEAR:
calendar.set(calendar.get(Calendar.YEAR), 0, 1, 0, 0, 0);
firstDate = calendar.getTime();
break;
case TYPE_MONTH:
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1, 0, 0, 0);
firstDate = calendar.getTime();
break;
case TYPE_WEEK:
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
firstDate = calendar.getTime();
break;
case TYPE_DAY:
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
0, 0, 0);
firstDate = calendar.getTime();
break;
}
return firstDate;
}
②按照类型获取 年月周日的最后一天
/**
* 获取日期的年月周日的最后一天
*
* @param type
* @param date
* @return
*/
public static Date endDayByDate(int type, Date date)
{
Date endDate = null;
int maxDay = 0;
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
switch (type)
{
case TYPE_YEAR:
int maxMonth = calendar.getActualMaximum(Calendar.MONTH);
maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(calendar.get(Calendar.YEAR), maxMonth, maxDay, 23, 59, 59);
endDate = calendar.getTime();
break;
case TYPE_MONTH:
maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), maxDay, 23, 59, 59);
endDate = calendar.getTime();
break;
case TYPE_WEEK:
calendar.add(Calendar.WEEK_OF_MONTH, 1);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
endDate = calendar.getTime();
break;
case TYPE_DAY:
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
23, 59, 59);
endDate = calendar.getTime();
break;
}
return endDate;
}
2、获取今年是哪一年
//获取今年是哪一年
public Integer getNowYear(Date date)
{
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return Integer.valueOf(gc.get(1));
}
3、获取本月是哪一个月
//获取本月是哪一月
public int getNowMonth(Date date)
{
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return gc.get(2) + 1;
}
4、根据传入的时间获取前一个时间的开始时间
①求开始时间
/**
* 根据传入的时间获取前一个时间的开始时间
*
* @param type 时间的类型(年月周日)
* @param startTime 时间的开始
* @param endTime 时间的结束
* @return
*/
public Date getLastStartTime(Integer type, Date startTime, Date endTime)
{
Date lastStartDate = null;
Calendar calendar = Calendar.getInstance();
calendar.setTime(startTime);
switch (type)
{
case 0: // 年
long millisecond = endTime.getTime() - startTime.getTime();
lastStartDate = new Date(startTime.getTime() - millisecond - 500);
break;
case 1: // 月
calendar.set(getNowYear(startTime), getNowMonth(startTime) - 2, 1);
lastStartDate = DateStatisticUtil.beginDayByDate(1, calendar.getTime());
break;
case 2: // 周
if (startTime == null)
{
return null;
}
calendar.setTime(startTime);
int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
if (dayofweek == 1)
{
dayofweek += 7;
}
calendar.add(Calendar.DATE, 2 - dayofweek - 7);
lastStartDate = DateStatisticUtil.beginDayByDate(2, calendar.getTime());
break;
case 3: // 日
Calendar cal = new GregorianCalendar();
cal.setTime(DateStatisticUtil.beginDayByDate(3, startTime));
cal.add(Calendar.DAY_OF_MONTH, -1);
lastStartDate = DateStatisticUtil.beginDayByDate(3, cal.getTime());
break;
case 4:// 时
lastStartDate = startTime;
break;
}
return lastStartDate;
}
②求结束时间
/**
* 根据传入的时间获取前一个时间的结束时间
*
* @param type
* @param startTime
* @param endTime
* @return
*/
public Date getLastEndTime(Integer type, Date startTime, Date endTime)
{
Date lastEndDate = null;
Calendar calendar = Calendar.getInstance();
calendar.setTime(endTime);
switch (type)
{
case 0: // 年
lastEndDate = new Date(startTime.getTime() - 1000);
break;
case 1: // 月
calendar.set(getNowYear(endTime), getNowMonth(endTime) - 2, 1);
int day = calendar.getActualMaximum(5);
calendar.set(getNowYear(endTime), getNowMonth(endTime) - 2, day);
lastEndDate = DateStatisticUtil.endDayByDate(1, calendar.getTime());
break;
case 2: // 周
calendar.setTime(getLastStartTime(2, startTime, endTime));
calendar.add(Calendar.DAY_OF_WEEK, 6);
Date weekEndSta = calendar.getTime();
lastEndDate = weekEndSta;
break;
case 3: // 日
Calendar cal = new GregorianCalendar();
cal.setTime(DateStatisticUtil.endDayByDate(3, endTime));
cal.add(Calendar.DAY_OF_MONTH, -1);
lastEndDate = DateStatisticUtil.endDayByDate(3, cal.getTime());
break;
case 4:// 时
lastEndDate = endTime;
break;
}
return lastEndDate;
}