日期:根据日期获取星期,查看是否满足日期格式,获取前30N天

朱雀 2021-09-03 04:11 853阅读 0赞

【一】根据日期获取星期

  1. private Integer dateToWeek(String datetime) {
  2. SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
  3. Integer[] weekDays = {7, 1, 2, 3, 4, 5, 6};
  4. Calendar cal = Calendar.getInstance();
  5. Date date;
  6. try {
  7. date = f.parse(datetime);
  8. cal.setTime(date);
  9. } catch (ParseException e) {
  10. LOGGER.error(HikLog.toLog(ErrorCodes.TIME_PARSE_ERROR.getCode(), ErrorCodes.TIME_PARSE_ERROR.getMessage()));
  11. e.printStackTrace();
  12. }
  13. int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
  14. if (w < 0) {
  15. w = 0;
  16. }
  17. return weekDays[w];
  18. }

【二】查看是否满足日期格式

  1. private static boolean isTimeLegal(String patternString) {
  2. 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])))))?$");
  3. Matcher result = pattern.matcher(patternString);
  4. if (result.matches()) {
  5. return true;
  6. } else {
  7. return false;
  8. }
  9. }

【三】获取前30N天

  1. private Date getTargetDay(Integer modelPeriod) {
  2. SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
  3. Date today = new Date();
  4. String date = fmt.format(today);
  5. Date minDate = null;
  6. Calendar calc = Calendar.getInstance();
  7. try {
  8. calc.setTime(fmt.parse(date));
  9. //BusinessConstant.MONTH为30,modelPeriod为周期,BusinessConstant.MONTH * modelPeriod为前30N天
  10. calc.add(Calendar.DATE, -(BusinessConstant.MONTH * modelPeriod));
  11. minDate = calc.getTime();
  12. } catch (ParseException e1) {
  13. LOGGER.error(HikLog.toLog(ErrorCodes.TIME_PARSE_ERROR.getCode(), ErrorCodes.TIME_PARSE_ERROR.getMessage()));
  14. e1.printStackTrace();
  15. }
  16. return minDate;
  17. }

发表评论

表情:
评论列表 (有 0 条评论,853人围观)

还没有评论,来说两句吧...

相关阅读