日期:根据日期获取星期,查看是否满足日期格式,获取前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;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值