好记性不如烂笔头
public class TimeUtil {
/**
* 获取 当前年、半年、季度、月、日、小时 开始结束时间
*/
private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
private final static SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");
private final static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private final static SimpleDateFormat _shortSdf = new SimpleDateFormat("yyyyMMdd");
/**
* 获得本周的第一天,周一
*
* @return
*/
public static Date getCurrentWeekDayStartTime() {
Calendar c = Calendar.getInstance();
try {
int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
c.add(Calendar.DATE, -weekday);
c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
} catch (Exception e) {
e.printStackTrace();
}
return c.getTime();
}
/**
* 获得目标日期的,周日
*
* @return
*/
public Date getCurrentWeekDayEndTime(String targetDate) {
Date from = DateUtils.parseDate(targetDate, DateUtils.FMT_DATE_);
Calendar cal = Calendar.getInstance();
cal.setTime(from);
try {
int weekday = cal.get(Calendar.DAY_OF_WEEK);
cal.add(Calendar.DATE, 8 - weekday);
cal.setTime(_shortSdf.parse(_shortSdf.format(cal.getTime())));
} catch (Exception e) {
e.printStackTrace();
}
return cal.getTime();
}
/**
* 获得本周的最后一天,周日
*
* @return
*/
public static Date getCurrentWeekDayEndTime() {
Calendar c = Calendar.getInstance();
try {
int weekday = c.get(Calendar.DAY_OF_WEEK);
c.add(Calendar.DATE, 8 - weekday);
c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));
} catch (Exception e) {
e.printStackTrace();
}
return c.getTime();
}
/**
* 获得本天的开始时间
*
* @return
*/
public static Date getCurrentDayStartTime() {
Date now = new Date();
try {
now = shortSdf.parse(shortSdf.format(now));
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 获得本天的结束时间
*
* @return
*/
public static Date getCurrentDayEndTime() {
Date now = new Date();
try {