/**
* 获取今日开始时间和结束时间
*
* @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
*/
public static Pair<Date,Date> getDayRange() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
Date startTime=calendar.getTime();
calendar.set(Calendar.HOUR_OF_DAY, 24);
calendar.set(Calendar.SECOND, -1);
Date stopTime=calendar.getTime();
return Pair.of(startTime,stopTime);
}
/**
* 获取昨天开始时间和结束时间
*
* @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
*/
public static Pair<Date,Date> getYesterdayRange() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, -24);
Date startTime=calendar.getTime();
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.HOUR_OF_DAY, 23);
Date stopTime=calendar.getTime();
return Pair.of(startTime,stopTime);
}
/**
* 获得最近三天的开始时间和结束时间
*
* @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
*/
public static Pair<Date,Date> getThreeRange() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 24);
Date stopTime=calendar.getTime();
calendar.set(Calendar.HOUR_OF_DAY, -72);
Date startTime=calendar.getTime();
return Pair.of(startTime,stopTime);
}
/**
* 获得近一周的开始时间和结束时间
*
* @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
*/
public static Pair<Date,Date> getDaySevenRange() {
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.HOUR_OF_DAY, 24);
Date stopTime=calendar.getTime();
calendar.set(Calendar.HOUR_OF_DAY, -168);
Date startTime=calendar.getTime();
return Pair.of(startTime,stopTime);
}
/**
* 获得近一月的开始时间和结束时间
*
* @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
*/
public static Pair<Date,Date> getDayTRange() {
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.HOUR_OF_DAY, 24);
Date stopTime=calendar.getTime();
calendar.set(Calendar.HOUR_OF_DAY, -720);
Date startTime=calendar.getTime();
return Pair.of(startTime,stopTime);
}
/**
* 获取本周开始时间和结束时间
*
* @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
*/
public static Pair<Date,Date> getWeekRange() {
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
Date startTime=calendar.getTime();
calendar.add(Calendar.DAY_OF_WEEK, 7);
calendar.set(Calendar.SECOND, -1);
Date stopTime=calendar.getTime();
return Pair.of(startTime,stopTime);
}
/**
* 获取上周开始时间和结束时间
*
* @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
*/
public static Pair<Date,Date> getLastWeekRange() {
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar.add(Calendar.DAY_OF_WEEK, -7);
Date startTime=calendar.getTime();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
calendar.add(Calendar.DAY_OF_WEEK, 6);
Date stopTime=calendar.getTime();
return Pair.of(startTime,stopTime);
}
/**
* 获取本月开始时间和结束时间
*
* @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
*/
public static Pair<Date,Date> getMonthRange() {
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
Date startTime=calendar.getTime();
calendar.add(Calendar.DAY_OF_WEEK, 7);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
// calendar.set(Calendar.HOUR_OF_DAY, 24);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
Date stopTime=calendar.getTime();
return Pair.of(startTime,stopTime);
}
/**
* 获取上月开始时间和结束时间
*
* @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
*/
public static Pair<Date,Date> getLastMonthRange() {
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
Date startTime=calendar.getTime();
calendar.add(Calendar.DAY_OF_WEEK, 7);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
//calendar.set(Calendar.HOUR_OF_DAY, 24);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
Date stopTime=calendar.getTime();
return Pair.of(startTime,stopTime);
}
/**
* 获取本季度开始时间和结束时间
*
* @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
*/
public static Pair<Date,Date> getQuarterRange() {
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
int currentMonth = calendar.get(Calendar.MONTH) + 1;
try {
if (currentMonth >= 1 && currentMonth <= 3)
calendar.set(Calendar.MONTH, 0);
else if (currentMonth >= 4 && currentMonth <= 6)
calendar.set(Calendar.MONTH, 3);
else if (currentMonth >= 7 && currentMonth <= 9)
calendar.set(Calendar.MONTH, 6);
else if (currentMonth >= 10 && currentMonth <= 12)
calendar.set(Calendar.MONTH, 9);
calendar.set(Calendar.DATE, 1);
} catch (Exception e) {
e.printStackTrace();
}
Date startTime=calendar.getTime();
calendar.add(Calendar.MONTH, 3);
calendar.set(Calendar.SECOND, -1);
Date stopTime=calendar.getTime();
return Pair.of(startTime,stopTime);
}
/**
* 获取上季度开始时间和结束时间
*
* @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
*/
public static Pair<Date,Date> getLastQuarterRange() {
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
int currentMonth = calendar.get(Calendar.MONTH) + 1;
try {
if (currentMonth >= 1 && currentMonth <= 3)
calendar.set(Calendar.MONTH, 0);
else if (currentMonth >= 4 && currentMonth <= 6)
calendar.set(Calendar.MONTH, 3);
else if (currentMonth >= 7 && currentMonth <= 9)
calendar.set(Calendar.MONTH, 6);
else if (currentMonth >= 10 && currentMonth <= 12)
calendar.set(Calendar.MONTH, 9);
calendar.set(Calendar.DATE, 1);
} catch (Exception e) {
e.printStackTrace();
}
calendar.add(Calendar.MONTH, -3);
Date startTime=calendar.getTime();
calendar.add(Calendar.MONTH, 3);
calendar.set(Calendar.SECOND, -1);
Date stopTime=calendar.getTime();
return Pair.of(startTime,stopTime);
}
/**
* 获取近半年开始时间和结束时间
*
* @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
*/
public static Pair<Date,Date> getHalfYearRange() {
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
calendar.add(Calendar.MONTH, -6);
Date startTime=calendar.getTime();
calendar.add(Calendar.MONTH, 6);
calendar.set(Calendar.SECOND, -1);
Date stopTime=calendar.getTime();
return Pair.of(startTime,stopTime);
}
/**
* 获取上半年开始时间和结束时间
*
* @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
*/
public static Pair<Date,Date> getUpHalfYearRange() {
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
Date startTime=calendar.getTime();
calendar.add(Calendar.MONTH, 6);
calendar.set(Calendar.SECOND, -1);
Date stopTime=calendar.getTime();
return Pair.of(startTime,stopTime);
}
/**
* 获取下半年开始时间和结束时间
*
* @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
*/
public static Pair<Date,Date> getNextHalfYearRange() {
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
calendar.add(Calendar.MONTH, 6);
Date startTime=calendar.getTime();
calendar.add(Calendar.MONTH, 6);
calendar.set(Calendar.SECOND, -1);
Date stopTime=calendar.getTime();
return Pair.of(startTime,stopTime);
}
/**
* 获取本年开始时间和结束时间
*
* @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
*/
public static Pair<Date,Date> getYearRange() {
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
Date startTime=calendar.getTime();
calendar.add(Calendar.YEAR, 1);
calendar.set(Calendar.SECOND, -1);
Date stopTime=calendar.getTime();
return Pair.of(startTime,stopTime);
}
/**
* 获取上一年开始时间和结束时间
*
* @return 返回 Pair 对象 left 是开始时间 right 是结束时间,类型为Date
*/
public static Pair<Date,Date> getLastYearRange() {
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
calendar.add(Calendar.YEAR, -1);
Date startTime=calendar.getTime();
calendar.add(Calendar.YEAR, 1);
calendar.set(Calendar.SECOND, -1);
Date stopTime=calendar.getTime();
return Pair.of(startTime,stopTime);
}
JAVA 获取今天、昨天、上周等日期
最新推荐文章于 2023-09-17 12:20:35 发布