Java工具类--持续更新

1.1 季度处理

1.1.1 获取当前季度str
1.1.2 获取前N个季度的str
1.1.3 校验目标时间date,是否超过了指定季度quarter的最后一天

/**
 * @ClassName : HandleQuarterUtil
 * @Description : 季度工具类
 */
public class HandleQuarterUtil {
    private static final String PATTERN = "yyyyMMdd";
 
    /*
     * @Description  获取当前季度str 如:202001为2020年第一季度
     * @Param []
     * @return java.lang.String
     **/
    public static String getThisQuarter() {
        Calendar now = Calendar.getInstance();
        String quarter = null;
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH) + 1;
        if (month == 1 || month == 2 || month == 3) {
            quarter = "01";
        } else if (month == 4 || month == 5 || month == 6) {
            quarter = "02";
        } else if (month == 7 || month == 8 || month == 9) {
            quarter = "03";
        } else if (month == 10 || month == 11 || month == 12) {
            quarter = "04";
        }
        return year + quarter;
    }
 
    /*
     * @Description  获取前N个季度的str
     * @Param [quarterNum] 要获取的季度个数
     * @return java.util.List<java.lang.String>
     **/
    public static List<String> getPastMultipleQuarters(int quarterNum) {
        Calendar now = Calendar.getInstance();
        List<String> quarters = new ArrayList<>(quarterNum);
        List<String> quarterCodes = new ArrayList<>();
        String quarter = null;
        for (int i = 1; i < quarterNum; i++) {
            String s1 = "quarter";
            s1 += i;
            quarterCodes.add(s1);
        }
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH) + 1;
        if (month == 1 || month == 2 || month == 3) {
            quarter = "01";
        } else if (month == 4 || month == 5 || month == 6) {
            quarter = "02";
        } else if (month == 7 || month == 8 || month == 9) {
            quarter = "03";
        } else if (month == 10 || month == 11 || month == 12) {
            quarter = "04";
        }
        for (int j = 0; j < quarterCodes.size(); j++) {
            String quarterj = quarterCodes.get(j);
            switch (quarter) {
                case "01":
                    quarterj = (year - 1 - j / 4) + "0" + (4 - j % 4);
                    break;
                case "02":
                    if (j == 0) {
                        quarterj = (year - 1 - j / 4) + "0" + (4 - j % 4);
                        break;
                    }
                    if (j % 4 == 0) {
                        quarterj = (year - j / 4) + "01";
                    } else {
                        quarterj = (year - 1 - j / 4) + "0" + (5 - j % 4);
                    }
                    break;
                case "03":
                    if (j == 0 || j == 1) {
                        quarterj = year + "0" + (2 - j);
                        break;
                    }
                    if ((j - 1) % 4 == 0) {
                        quarterj = (year - j / 4) + "01";
                    } else if (j % 4 == 0) {
                        quarterj = (year - j / 4) + "02";
                    } else {
                        quarterj = (year - 1 - j / 4) + "0" + (6 - j % 4);
                    }
                    break;
                case "04":
                    if (j == 0 || j == 1 || j == 2) {
                        quarterj = year + "0" + (3 - j);
                        break;
                    }
                    if ((j - 2) % 4 == 0) {
                        quarterj = (year - j / 4) + "01";
                    } else if ((j - 1) % 4 == 0) {
                        quarterj = (year - j / 4) + "02";
                    } else if (j % 4 == 0) {
                        quarterj = (year - j / 4) + "03";
                    } else {
                        quarterj = (year - 1 - j / 4) + "0" + (7 - j % 4);
                    }
                    break;
                default:
                    break;
            }
            quarters.add(quarterj);
        }
        return quarters;
    }
 
 
    public static String formatDate(Date currentDate, String partten) {
        if (StringUtils.isEmpty(partten) || currentDate == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(partten);
        return sdf.format(currentDate);
    }
 

    /*
     * @Description  校验目标时间date,是否超过了指定季度quarter的最后一天
     * @Param [date, quarter]
     * @return boolean
     * eg:isInQuarter(new Date(), "202203");
     **/
    public static boolean isInQuarter(Date date, String quarter) {
        String[] s = new String[2];
        String str;
        String yearStr = quarter.substring(0, 4);
        int quarterInt = Integer.valueOf(quarter.substring(quarter.length() - 1, quarter.length()));
        //设置本年的季度
        Calendar quarterCalendar;
        switch (quarterInt) {
            case 1://当年经过了一个季度,加上前4个季度
                quarterCalendar = Calendar.getInstance();
                quarterCalendar.set(Calendar.MONTH, 3);
                quarterCalendar.set(Calendar.DATE, 1);
                quarterCalendar.set(Calendar.DATE, -1);
                str = formatDate(quarterCalendar.getTime(), PATTERN);
                s[0] = str.substring(0, str.length() - 5) + "0101";//季度第一天
                s[1] = str.substring(0, str.length() - 5) + "0331";//最后一天
                break;
            case 2://当年经过了二个季度,加上前3个季度
                quarterCalendar = Calendar.getInstance();
                quarterCalendar.set(Calendar.MONTH, 6);
                quarterCalendar.set(Calendar.DATE, 1);
                quarterCalendar.set(Calendar.DATE, -1);
                str = formatDate(quarterCalendar.getTime(), PATTERN);
                s[0] = str.substring(0, str.length() - 5) + "0401";//季度第一天
                s[1] = str.substring(0, str.length() - 5) + "0630";//最后一天
                break;
            case 3://当年经过了三个季度,加上前2个季度
                quarterCalendar = Calendar.getInstance();
                quarterCalendar.set(Calendar.MONTH, 9);
                quarterCalendar.set(Calendar.DATE, 1);
                quarterCalendar.set(Calendar.DATE, -1);
                str = formatDate(quarterCalendar.getTime(), PATTERN);
                s[0] = str.substring(0, str.length() - 5) + "0701";//季度第一天
                s[1] = str.substring(0, str.length() - 5) + "0930";//最后一天
                break;
            case 4://当年经过了四个季度,加上前一个季度
                quarterCalendar = Calendar.getInstance();
                quarterCalendar.set(Calendar.MONTH, 3);
                quarterCalendar.set(Calendar.DATE, 1);
                quarterCalendar.set(Calendar.DATE, -1);
                str = formatDate(quarterCalendar.getTime(), PATTERN);
                s[0] = str.substring(0, str.length() - 5) + "1001";//季度第一天
                s[1] = str.substring(0, str.length() - 5) + "1231";//最后一天
                break;
            default:
                break;
        }
        int targetQuarterLastDay = Integer.valueOf(yearStr + s[1].substring(s[1].length() - 4, s[1].length()));
        int targetDate = Integer.valueOf(formatDate(date, PATTERN));
        if (targetDate > targetQuarterLastDay) {
            return false;
        }
        return true;
    }
}

个人博客:nornless.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Himma_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值