欢迎光临
一个废柴的博客

Java中获取日期所在季度工具类

Java中涉及获取指定日期所在季度的工具类方法整理如下:

import lc.jw.loushang.util.DateTool;
import org.apache.commons.lang.StringUtils;

import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalQuery;
import java.util.Arrays;
import java.util.Calendar;

/**
 * @Auther:IT废柴-https://www.itfeichai.com
 */
public class QuarterExample {


    enum Quarter {
        FIRST, SECOND, THIRD, FOURTH
    }

    class QuarterOfYearQuery implements TemporalQuery<Quarter> {
        @Override
        public QuarterExample.Quarter queryFrom(TemporalAccessor temporal) {
            LocalDate now = LocalDate.from(temporal);
            if (now.isBefore(now.with(Month.APRIL).withDayOfMonth(1))) {
                return QuarterExample.Quarter.FIRST;
            } else if (now.isBefore(now.with(Month.JULY).withDayOfMonth(1))) {
                return QuarterExample.Quarter.SECOND;
            } else if (now.isBefore(now.with(Month.OCTOBER).withDayOfMonth(1))) {
                return QuarterExample.Quarter.THIRD;
            } else {
                return QuarterExample.Quarter.FOURTH;
            }
        }
    }

    /**
     * 根据时间字符串获取季度
     *
     * @param dateStr "YYYY-01-12"
     * @return
     */
    public int getQuarter(String dateStr) {
        if (StringUtils.isNotBlank(dateStr)) {
            LocalDate date = LocalDate.parse(dateStr);
            TemporalQuery<Quarter> quarterOfYearQuery = new QuarterExample.QuarterOfYearQuery();
            QuarterExample.Quarter quarter = date.query(quarterOfYearQuery);
            if (QuarterExample.Quarter.FIRST == quarter) {
                return 1;
            }
            if (QuarterExample.Quarter.SECOND == quarter) {
                return 2;
            }
            if (QuarterExample.Quarter.THIRD == quarter) {
                return 3;
            }

            if (QuarterExample.Quarter.FOURTH == quarter) {
                return 4;
            }
        }
        return 0;
    }

    /**
     * 将季度数字转为文本(1,2转为第一季度、第二季度)
     *
     * @return
     */
    public static String convertCircleNumStrToText(String circle) {
        if (StringUtils.isNotBlank(circle)) {
            String[] cArray = circle.split(",");
            for (int i = 0; i < cArray.length; i++) {
                if ("1".equals(cArray[i])) {
                    cArray[i] = "第一季度";
                }
                if ("2".equals(cArray[i])) {
                    cArray[i] = "第二季度";
                }
                if ("3".equals(cArray[i])) {
                    cArray[i] = "第三季度";
                }
                if ("4".equals(cArray[i])) {
                    cArray[i] = "第四季度";
                }
            }
            return StringUtils.join(cArray, "、");
        }
        return "";
    }

    /**
     * 根据给定的年份和季度,获取相对给定季度的最近四个季度(包括给定季度)
     *
     * @param year
     * @param quarter
     * @return
     */
    public static String[] getLastFourQuarters(String year, String quarter) {
        String[] jiduArray = new String[4];
        jiduArray[3] = year + "," + quarter;
        int curYear = Integer.parseInt(year);
        int curQuarter = Integer.parseInt(quarter);
        for (int i = 1; i <= 3; i++) {
            if (curQuarter - 1 > 0) {
                jiduArray[3 - i] = curYear + "," + (curQuarter - 1);
                curQuarter = curQuarter - 1;
            } else {
                curYear = curYear - 1;
                curQuarter = 4;
                jiduArray[3 - i] = curYear + "," + 4;
            }
        }
        return jiduArray;
    }

    /**
     * 获取某年某季度的第一天和最后一天
     *
     * @param quarter 第几季度
     */
    public static String[] getQuarterRange(int year, int quarter) {
        String[] s = new String[2];
        String str = "";
        // 设置本年的季
        Calendar quarterCalendar = null;
        switch (quarter) {
            case 1: // 本年到现在经过了一个季度,在加上前4个季度
                quarterCalendar = Calendar.getInstance();
                quarterCalendar.set(Calendar.YEAR, year);
                quarterCalendar.set(Calendar.MONTH, 3);
                quarterCalendar.set(Calendar.DATE, 1);
                quarterCalendar.add(Calendar.DATE, -1);
                str = DateTool.formatDate(quarterCalendar.getTime());
                s[0] = str.substring(0, str.length() - 5) + "01-01";
                s[1] = str;
                break;
            case 2: // 本年到现在经过了二个季度,在加上前三个季度
                quarterCalendar = Calendar.getInstance();
                quarterCalendar.set(Calendar.YEAR, year);
                quarterCalendar.set(Calendar.MONTH, 6);
                quarterCalendar.set(Calendar.DATE, 1);
                quarterCalendar.add(Calendar.DATE, -1);
                str = DateTool.formatDate(quarterCalendar.getTime());
                s[0] = str.substring(0, str.length() - 5) + "04-01";
                s[1] = str;
                break;
            case 3:// 本年到现在经过了三个季度,在加上前二个季度
                quarterCalendar = Calendar.getInstance();
                quarterCalendar.set(Calendar.YEAR, year);
                quarterCalendar.set(Calendar.MONTH, 9);
                quarterCalendar.set(Calendar.DATE, 1);
                quarterCalendar.add(Calendar.DATE, -1);
                str = DateTool.formatDate(quarterCalendar.getTime());
                s[0] = str.substring(0, str.length() - 5) + "07-01";
                s[1] = str;
                break;
            case 4:// 本年到现在经过了四个季度,在加上前一个季度
                quarterCalendar = Calendar.getInstance();
                quarterCalendar.set(Calendar.YEAR, year);
                str = DateTool.formatDate(quarterCalendar.getTime());
                s[0] = str.substring(0, str.length() - 5) + "10-01";
                s[1] = str.substring(0, str.length() - 5) + "12-31";
                break;
        }
        return s;
    }

    public static void main(String[] args) {
        System.out.println("1.根据时间字符串获取对应的季度");
        System.out.println(new QuarterExample().getQuarter("2018-11-27"));
        System.out.println("2.将数字季度转换为文本");
        System.out.println(new QuarterExample().convertCircleNumStrToText("3"));
        System.out.println("3.根据给定的年份和季度,获取相对给定季度的最近四个季度(包括给定季度)");
        System.out.println(StringUtils.join(new QuarterExample().getLastFourQuarters("2018", "2"),"、"));
        System.out.println("4.获取某年某季度的第一天和最后一天");
        System.out.println(Arrays.toString(new QuarterExample().getQuarterRange(2018,3)));
    }
}

打印测试结果:

1.根据时间字符串获取对应的季度
4
2.将数字季度转换为文本
第三季度
3.根据给定的年份和季度,获取相对给定季度的最近四个季度(包括给定季度)
2017,3、2017,4、2018,1、2018,2
4.获取某年某季度的第一天和最后一天
[2018-07-01, 2018-09-30]

参考:https://www.w3cschool.cn/java/codedemo-484047537.html;

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址