日期:根据日期获取星期,查看是否满足日期格式,获取前30N天
【一】根据日期获取星期
private Integer dateToWeek(String datetime) {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Integer[] weekDays = {7, 1, 2, 3, 4, 5, 6};
Calendar cal = Calendar.getInstance();
Date date;
try {
date = f.parse(datetime);
cal.setTime(date);
} catch (ParseException e) {
LOGGER.error(HikLog.toLog(ErrorCodes.TIME_PARSE_ERROR.getCode(), ErrorCodes.TIME_PARSE_ERROR.getMessage()));
e.printStackTrace();
}
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0) {
w = 0;
}
return weekDays[w];
}
【二】查看是否满足日期格式
private static boolean isTimeLegal(String patternString) {
Pattern pattern = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s((([0-1][0-9])|(2?[0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
Matcher result = pattern.matcher(patternString);
if (result.matches()) {
return true;
} else {
return false;
}
}
【三】获取前30N天
private Date getTargetDay(Integer modelPeriod) {
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
Date today = new Date();
String date = fmt.format(today);
Date minDate = null;
Calendar calc = Calendar.getInstance();
try {
calc.setTime(fmt.parse(date));
//BusinessConstant.MONTH为30,modelPeriod为周期,BusinessConstant.MONTH * modelPeriod为前30N天
calc.add(Calendar.DATE, -(BusinessConstant.MONTH * modelPeriod));
minDate = calc.getTime();
} catch (ParseException e1) {
LOGGER.error(HikLog.toLog(ErrorCodes.TIME_PARSE_ERROR.getCode(), ErrorCodes.TIME_PARSE_ERROR.getMessage()));
e1.printStackTrace();
}
return minDate;
}
还没有评论,来说两句吧...