1、获取两个时间之间的天数、周数、月数(最小单位为天)有三种实现方式:calendar的date日期获取出来,通过until方法获取之间的差距;转化为毫秒数,再除以一天得毫秒数;字符串的date日期获取然后until获取差距,其中通过java的微基准测试发现方式2的性能最好,方式3的性能最差
public static int getMonthsDiff(Date startDate, Date endDate) {
Calendar calendar = Calendar.getInstance(Locale.CHINA);
calendar.setTime(startDate);
Calendar calendar2 = Calendar.getInstance(Locale.CHINA);
calendar2.setTime(endDate);
Integer startYear = calendar.get(Calendar.YEAR);
Integer startMonth = calendar.get(Calendar.MONTH)+1;
Integer startDay = calendar.get(Calendar.DATE);
Integer endYear = calendar2.get(Calendar.YEAR);
Integer endMonth = calendar2.get(Calendar.MONTH)+1;
Integer endDay = calendar2.get(Calendar.DATE);
LocalDate endLocalDate = LocalDate.of(endYear,endMonth,endDay);
LocalDate startLocalDate = LocalDate.of(startYear,startMonth,startDay);
return (int) startLocalDate.until(endLocalDate, ChronoUnit.MONTHS);
}
其中修改最后一行代码的 ChronoUnit.MONTHS为其它参数则可统计两个时间之差的天或者周了
// 计算两个日期相隔的天数
public int nDaysBetweenTwoDate(Date firstString, Date secondString) {
int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
return nDay;
}
String[] startStr = new SimpleDateFormat("yyyy-MM-dd").format(startDate).split("-");
String[] endStr = new SimpleDateFormat("yyyy-MM-dd").format(endDate).split("-");
Integer startYear = Integer.parseInt(startStr[0]);
Integer startMonth = Integer.parseInt(startStr[1]);
Integer startDay = Integer.parseInt(startStr[2]);
Integer endYear = Integer.parseInt(endStr[0]);
Integer endMonth = Integer.parseInt(endStr[1]);
Integer endDay = Integer.parseInt(endStr[2]);
LocalDate endLocalDate = LocalDate.of(endYear,endMonth,endDay);
LocalDate startLocalDate = LocalDate.of(startYear,startMonth,startDay);
return startLocalDate.until(endLocalDate, ChronoUnit.MONTHS);
经过jmh微基准测试方式1和方式2结果如下:
性能相差了十几倍,所以如果需要以秒为最小计算单位来计算天数的话还是首选时间戳的方式
2、判断当前时间是否在[startTime, endTime]区间
public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
if (nowTime.getTime() == startTime.getTime()|| nowTime.getTime() == endTime.getTime()) {
return true;
}
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar begin = Calendar.getInstance();
begin.setTime(startTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
if (date.after(begin) && date.before(end)) {
return true;
} else {
return false;
}
}
3、获取当前时间属于第几周
public static int getWeekOfYear(Date date) {
if (date == null) {
date = new Date();
}
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
int week = calendar.get(Calendar.WEEK_OF_YEAR);
return week;
}
4、获取最近几个月的月份集合和获取两个时间之间的日期集合
public static List getRecentMonths(int size, boolean isContainsCurMonth) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar c = Calendar.getInstance();
c.setTime(new Date());
List list = new ArrayList(size);
int start = 0, end = size;
if (!isContainsCurMonth) {
start = 1;
end = size + 1;
}
for (int i = start; i < end; i++) {
c.setTime(new Date());
c.add(Calendar.MONTH, -i);
Date m = c.getTime();
list.add(sdf.format(m));
}
Collections.reverse(list);
return list;
}
public static List getDatesBetweenTwoDate(String startDate, String endDate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
List list = new ArrayList<>();
Date date_start = sdf.parse(startDate);
Date date_end = sdf.parse(endDate);
Date date = date_start;
Calendar cd = Calendar.getInstance();
while (date.getTime() <= date_end.getTime()) {
list.add(sdf.format(date));
cd.setTime(date);
cd.add(Calendar.DATE, 1);
date = cd.getTime();
}
return list;
}