import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.yoyowang.framework.utils.helper.DateHelper;
public class DateUtil {
// 获得当天0点时间
public static Date getTimesmorning() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
// 获得昨天0点时间
public static Date getYesterdaymorning() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(getTimesmorning().getTime()-3600*24*1000);
return cal.getTime();
}
// 获得当天近7天时间
public static Date getWeekFromNow() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis( getTimesmorning().getTime()-3600*24*1000*7);
return cal.getTime();
}
// 获得当天24点时间
public static Date getTimesnight() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 24);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
// 获得本周一0点时间
public static Date getTimesWeekmorning() {
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return cal.getTime();
}
// 获得本周日24点时间
public static Date getTimesWeeknight() {
Calendar cal = Calendar.getInstance();
cal.setTime(getTimesWeekmorning());
cal.add(Calendar.DAY_OF_WEEK, 7);
return cal.getTime();
}
// 获得本月第一天0点时间
public static Date getTimesMonthmorning() {
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
return cal.getTime();
}
// 获得本月最后一天24点时间
public static Date getTimesMonthnight() {
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
cal.set(Calendar.HOUR_OF_DAY, 24);
return cal.getTime();
}
public static Date getLastMonthStartTime() {
Calendar cal = Calendar.getInstance();
cal.setTime(getTimesMonthmorning());
cal.add(Calendar.MONTH, -1);
return cal.getTime();
}
public static Date getLastMonthEndTime() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE,59);
cal.set(Calendar.SECOND,59);
return cal.getTime();
}
//获取当前季度开始时间
public static Date getCurrentQuarterStartTime() {
Calendar c = Calendar.getInstance();
int currentMonth = c.get(Calendar.MONTH) + 1;
SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
Date now = null;
try {
if (currentMonth >= 1 && currentMonth <= 3)
c.set(Calendar.MONTH, 0);
else if (currentMonth >= 4 && currentMonth <= 6)
c.set(Calendar.MONTH, 3);
else if (currentMonth >= 7 && currentMonth <= 9)
c.set(Calendar.MONTH, 4);
else if (currentMonth >= 10 && currentMonth <= 12)
c.set(Calendar.MONTH, 9);
c.set(Calendar.DATE, 1);
now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
//获取当前季度结束时间
public static Date getCurrentQuarterEndTime() {
Calendar cal = Calendar.getInstance();
cal.setTime(getCurrentQuarterStartTime());
cal.add(Calendar.MONTH, 3);
return cal.getTime();
}
/**
* @Description:获取上季度开始时间
*/
public static Date getLastQuarterStartTime() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, ((int) cal.get(Calendar.MONTH) / 3 - 1) * 3);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.SECOND,0);
return cal.getTime();
}
/**
* @Description:获取上季度结束时间
*/
public static Date getLastQuarterEndTime() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, ((int) cal.get(Calendar.MONTH) / 3 - 1) * 3 + 2);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE,59);
cal.set(Calendar.SECOND,59);
return cal.getTime();
}
//获取今年开始时间
public static Date getCurrentYearStartTime() {
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR), 0, 0, 0, 0, 0);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.YEAR));
return cal.getTime();
}
//获取明年开始时间
public static Date getCurrentYearEndTime() {
Calendar cal = Calendar.getInstance();
cal.setTime(getCurrentYearStartTime());
cal.add(Calendar.YEAR, 1);
return cal.getTime();
}
//获取去年开始时间
public static Date getLastYearStartTime() {
Calendar cal = Calendar.getInstance();
cal.setTime(getCurrentYearStartTime());
cal.add(Calendar.YEAR, -1);
return cal.getTime();
}
//获取去年结束时间
public static Date getLastYearEndTime() {
Calendar cal = Calendar.getInstance();
Integer year = cal.get(Calendar.YEAR) - 1;
return DateHelper.formatStringToDate(year + "-12-31 23:59:59", "yyyy-MM-dd HH:mm:ss");
}
//获取上周一时间
public static Date getLastWeekMondayStartTime() {
Calendar cal = Calendar.getInstance();
cal.setTime(getTimesWeekmorning());
cal.add(Calendar.DATE, -7);
return cal.getTime();
}
//获取上周末时间
public static Date getLastWeekSundayEndTime() {
Calendar cal = Calendar.getInstance();
cal.setTime(getTimesWeekmorning());
cal.add(Calendar.DATE, -1);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE,59);
cal.set(Calendar.SECOND,59);
return cal.getTime();
}
//获取上一旬开始时间
public static Date getLastXunStartTime() {
Calendar cal = Calendar.getInstance();
Integer day = cal.get(Calendar.DAY_OF_MONTH);
if (day > 20) {
cal.set(Calendar.DAY_OF_MONTH, 11);
} else if (day > 10) {
cal.set(Calendar.DAY_OF_MONTH, 1);
} else {
cal.add(Calendar.MONTH, -1);
cal.set(Calendar.DAY_OF_MONTH, 21);
}
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.SECOND,0);
return cal.getTime();
}
//获取上一旬结束时间
public static Date getLastXunEndTime() {
Calendar cal = Calendar.getInstance();
Integer day = cal.get(Calendar.DAY_OF_MONTH);
if (day > 20) {
cal.set(Calendar.DAY_OF_MONTH, 20);
} else if (day > 10) {
cal.set(Calendar.DAY_OF_MONTH, 10);
} else {
cal.add(Calendar.MONTH, -1);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
}
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE,59);
cal.set(Calendar.SECOND,59);
return cal.getTime();
}
/**
* @Description:获取时间段内的所有日期
*/
public static List<Date> findDates(Date dBegin, Date dEnd) {
List<Date> lDate = new ArrayList<>();
//lDate.add(dBegin);
Calendar calBegin = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(dBegin);
Calendar calEnd = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(dEnd);
// 测试此日期是否在指定日期之后
while (dEnd.after(calBegin.getTime())) {
lDate.add(calBegin.getTime());
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.DAY_OF_MONTH, 1);
}
return lDate;
}
/**
* @Description:获取时间段内的所有日期和小时
*/
public static List<Date> findDatesAndHours(Date dBegin, Date dEnd) {
List<Date> lDate = new ArrayList<>();
//lDate.add(dBegin);
Calendar calBegin = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(dBegin);
Calendar calEnd = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(dEnd);
// 测试此日期是否在指定日期之后
while (dEnd.after(calBegin.getTime())) {
lDate.add(calBegin.getTime());
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.HOUR_OF_DAY, 1);
}
return lDate;
}
/**
* @Description:获取时间段内的所有周
*/
public static List<String> findWeeks(Date dBegin, Date dEnd) {
List<String> list = new ArrayList<>();
Calendar calBegin = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(dBegin);
Calendar calEnd = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(dEnd);
// 测试此日期是否在指定日期之后
while (dEnd.after(calBegin.getTime())) {
list.add(calBegin.get(Calendar.YEAR) + "" + getWeekOfYear(new Date(calBegin.getTimeInMillis())));
calBegin.add(Calendar.DAY_OF_MONTH, 1);
}
List listTemp = new ArrayList();
for(int i=0;i<list.size();i++){
if(!listTemp.contains(list.get(i))){
listTemp.add(list.get(i));
}
}
return listTemp;
}
//获取日期是一年中的第几周
public static Integer getWeekOfYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int week = calendar.get(Calendar.WEEK_OF_YEAR);
int mouth = calendar.get(Calendar.MONTH);
if (mouth >= 11 && week <= 1) {
week += 52;
}
return week;
}
//根据年月获取开始日期
public static Date getBeginTime(int year, int month) {
YearMonth yearMonth = YearMonth.of(year, month);
LocalDate localDate = yearMonth.atDay(1);
LocalDateTime startOfDay = localDate.atStartOfDay();
ZonedDateTime zonedDateTime = startOfDay.atZone(ZoneId.of("Asia/Shanghai"));
return Date.from(zonedDateTime.toInstant());
}
//根据年月获取结束日期
public static Date getEndTime(int year, int month) {
YearMonth yearMonth = YearMonth.of(year, month);
LocalDate endOfMonth = yearMonth.atEndOfMonth();
LocalDateTime localDateTime = endOfMonth.atTime(23, 59, 59, 999);
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
return Date.from(zonedDateTime.toInstant());
}
}
Java DateUtil 工具类
最新推荐文章于 2020-09-10 17:32:02 发布