滚表获取表名公共方法

 public static void main(String[] args) {
        log.info("20200903={}", getOrderRecordTableNameRunByStr("20200903"));
        log.info("20200901={}", getOrderRecordTableNameRunByStr("20200901"));
        log.info("20200831={}", getOrderRecordTableNameRunByStr("20200831"));
        log.info("20200731={}", getOrderRecordTableNameRunByStr("20200731"));
        log.info("20200603={}", getOrderRecordTableNameRunByStr("20200603"));
        log.info("20190903={}", getOrderRecordTableNameRunByStr("20190903"));
    }


 /**
     * 根据时间获取表名
     * 1.日期距离当日小于等于3天 日表
     * 2.月份距离当月小于等于2个月 ,走月表
     * 3.月份距离当月大于2个月 年表
     *
     * @param dateStr dateStr
     * @return String
     */
    public static String getOrderRecordTableNameRunByStr(String dateStr) {
        if (isDistance4DayNum(DAY_NUM, dateStr)) {
            //2天内,日表

            return getTableName2DayTable4Str(dateStr);
        } else if (isDistance4MonthNum(MONTH_NUM, dateStr)) {
            //2月内,月表

            return getTableName2MonthTable4Str(dateStr);
        } else {
            //年表

            return getTableName4Str(ORDER_RECORD_YEAR, YEAR_FORMAT, dateStr);
        }
    }



 /**
     * 比较距离当前多少天
     *
     * @param dayNum dayNum
     * @param date   date
     * @return boolean
     */
    public static boolean isDistance4DayNum(int dayNum, String date) {
        Date inDate = null;
        try {
            inDate = new SimpleDateFormat(DATE_FORMAT).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long inLong = inDate.getTime();
        return dayNum > getDistanceDayNum(inLong);
    }


 /**
     * 判断日期距离当前日期多少天
     *
     * @param timeStamp
     * @return
     */
    private static int getDistanceDayNum(long timeStamp) {
        long curTime = System.currentTimeMillis();
        long time = curTime - timeStamp;
        return (int) (time / 1000 / 3600 / 24);
    }



 /**
     * 比较距离当前多少月
     *
     * @param monthNum monthNum
     * @param date     date
     * @return boolean
     */
    private static boolean isDistance4MonthNum(int monthNum, String date) {
        Date inDate = null;
        try {
            inDate = new SimpleDateFormat(DATE_FORMAT).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return monthNum > getDistanceMonthNum(inDate);
    }


  /**
     * 判断日期距离当前日期多少月
     *
     * @param inDate
     * @return
     */
    private static int getDistanceMonthNum(Date inDate) {
        Calendar calNow = Calendar.getInstance();
        Calendar calIn = Calendar.getInstance();
        calIn.setTime(inDate);
        if (calNow.get(Calendar.YEAR) < calIn.get(Calendar.YEAR)) {
            //年份异常
            throw new RuntimeException("日期年份异常!");
        }
        return (calNow.get(Calendar.MONTH) + 1) - (calIn.get(Calendar.MONTH) + 1) + (12 * (calNow.get(Calendar.YEAR) - calIn.get(Calendar.YEAR)));
    }

 

你可能感兴趣的:(java代码,分表表明获取公共方法)