给定指定日期
1. 是否工作时间: 非节假日、调休的周末为工作日,工作日早上9点之后,晚上9点之前为工作时间
2.获取两个日期的的差,返回秒
3.获取两个日期的的差,返回分钟
4.获取两个日期的的差,返回天数
5. 将天数以年来描述
6. 将分钟以区间来描述
7.是否是同一天
private final static Set<String> HOLIDAYS = new HashSet<>();
private final static Set<String> WORKDAY_AT_WEEKEND = new HashSet<>();
public static boolean isWorkHours(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
String today = getWebDateString(date);
int day = calendar.get(Calendar.DAY_OF_WEEK);
if (HOLIDAYS.contains(today)) {
return false;
}
if (!WORKDAY_AT_WEEKEND.contains(today) && (day == 1 || day == 7)) {
return false;
}
int hours = calendar.get(Calendar.HOUR_OF_DAY);
if (hours < 9 || hours > 21) {
return false;
}
return true;
}
public static String getWebDateString(Date date) {
DateFormat dateFormat = getNewDateFormat("yyyy-MM-dd");
return getDateString(date, dateFormat);
}
public static String getDateString(Date date, DateFormat dateFormat) {
return date != null && dateFormat != null ? dateFormat.format(date) : null;
}
public static DateFormat getNewDateFormat(String pattern) {
DateFormat df = new SimpleDateFormat(pattern);
df.setLenient(false);
return df;
}
public static Long subSecond(Date date1, Date date2) {
return Math.abs(date1.getTime() - date2.getTime()) / 1000;
}
public static Long subMinute(Date date1, Date date2) {
return Math.abs((date1.getTime() - date2.getTime()) / 60000);
}
public static int subDay(Date date1, Date date2) {
return Math.round(Math.abs((date1.getTime() - date2.getTime()) / (60000 * 60 * 24)));
}
public static String dayToYearsDesc(int days) {
String desc;
if (0 <= days && days <= 180) {
desc = "半年内";
} else if (180 < days && days <= 365) {
desc = "一年内";
} else if (365 < days && days <= 545) {
desc = "一年到一年半";
} else if (545 < days && days <= 730) {
desc = "一年半到两年";
} else if (730 < days && days <= 1095) {
desc = "两年到三年";
} else if (1095 < days && days <= 1460) {
desc = "三年到四年";
} else if (1460 < days && days <= 1825) {
desc = "四年到五年";
} else {
desc = "大于五年";
}
return desc;
}
public static String secondToInterval(Integer secondNum) {
String interval;
if (0 < secondNum && secondNum <= 60) {
interval = "1分钟以内";
} else if (60 < secondNum && secondNum <= 180) {
interval = "1-3分钟";
} else if (180 < secondNum && secondNum <= 300) {
interval = "3-5分钟";
} else if (300 < secondNum && secondNum <= 900) {
interval = "5-15分钟";
} else if (900 < secondNum && secondNum <= 1800) {
interval = "15-30分钟";
} else if (1800 < secondNum && secondNum <= 3600) {
interval = "30-60分钟";
} else if (3600 < secondNum && secondNum <= 7200) {
interval = "1-2小时";
} else if (secondNum > 120) {
interval = "2小时以上";
} else {
interval = "0";
}
return interval;
}
public static boolean isSameDate(Date d1, Date d2) {
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(d1);
c2.setTime(d2);
return (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR))
&& (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH))
&& (c1.get(Calendar.DAY_OF_MONTH) == c2.get(Calendar.DAY_OF_MONTH));
}