java 各个时间断维度计算

java 各个时间断维度计算,第1张

java 各个时间断维度计算

        

1. 自然周中的起止时间

public static Map getWeek(){
        Map map = new HashMap<>();
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
        String beginTime = format.format(c.getTime());
        map.put("startDate", beginTime + " 00:00:00");
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
        String endTime = format.format(c.getTime());
        map.put("endDate", endTime + " 23:59:59");
        return map;
    }

2. 获取当前月第一天 - 获取当前月最后一天

public static Map getMonth(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Map map = new HashMap<>();
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        String beginTime = format.format(c.getTime());
        map.put("startDate", beginTime + " 00:00:00");
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        String endTime = format.format(ca.getTime());
        map.put("endDate", endTime + " 23:59:59");
        return map;
    }

3. 获取某季度的第一天和最后一天

public static Integer getQuarter() {
        Calendar c = Calendar.getInstance();
        Integer month = c.get(c.MONTH) + 1;
        Integer quarter = 0;
        if (month >= 1 && month <= 3) {
            quarter = 1;
        } else if (month >= 4 && month <= 6) {
            quarter = 2;
        } else if (month >= 7 && month <= 9) {
            quarter = 3;
        } else {
            quarter = 4;
        }
        return quarter;
    }


public static Map getCurrQuarter(int num) {
        Map map = new HashMap<>();
        String str = "";
        // 设置本年的季
        Calendar quarterCalendar = null;
        switch (num) {
            case 1: // 本年到现在经过了一个季度,在加上前4个季度
                quarterCalendar = Calendar.getInstance();
                quarterCalendar.set(Calendar.MONTH, 3);
                quarterCalendar.set(Calendar.DATE, 1);
                quarterCalendar.add(Calendar.DATE, -1);
                str = DateUtils.formatDate(quarterCalendar.getTime(), "yyyy-MM-dd");
                map.clear();
                map.put("startDate",str.substring(0, str.length() - 5) + "01-01" + " 00:00:00");
                map.put("endDate",str + " 23:59:59");
                break;
            case 2: // 本年到现在经过了二个季度,在加上前三个季度
                quarterCalendar = Calendar.getInstance();
                quarterCalendar.set(Calendar.MONTH, 6);
                quarterCalendar.set(Calendar.DATE, 1);
                quarterCalendar.add(Calendar.DATE, -1);
                str = DateUtils.formatDate(quarterCalendar.getTime(), "yyyy-MM-dd");
                map.clear();
                map.put("startDate",str.substring(0, str.length() - 5) + "04-01" + " 00:00:00");
                map.put("endDate",str + " 23:59:59");
                break;
            case 3:// 本年到现在经过了三个季度,在加上前二个季度
                quarterCalendar = Calendar.getInstance();
                quarterCalendar.set(Calendar.MONTH, 9);
                quarterCalendar.set(Calendar.DATE, 1);
                quarterCalendar.add(Calendar.DATE, -1);
                str = DateUtils.formatDate(quarterCalendar.getTime(), "yyyy-MM-dd");
                map.clear();
                map.put("startDate",str.substring(0, str.length() - 5) + "07-01" + " 00:00:00");
                map.put("endDate",str + " 23:59:59");
                break;
            case 4:// 本年到现在经过了四个季度,在加上前一个季度
                quarterCalendar = Calendar.getInstance();
                str = DateUtils.formatDate(quarterCalendar.getTime(), "yyyy-MM-dd");
                map.clear();
                map.put("startDate",str.substring(0, str.length() - 5) + "10-01" + " 00:00:00");
                map.put("endDate",str.substring(0, str.length() - 5) + "12-31" + " 23:59:59");
                break;
        }
        return map;
    }

4. 获取当年的第一天 - 获取当年的最后一天

public static Map getYear(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Map map = new HashMap<>();
        String str = sdf.format(new Date());
        map.put("startDate",str + "-01-01" + " 00:00:00");
        map.put("endDate",str+ "-12-31" + " 23:59:59");
        map.put("year",str);
        return map;
    }

5. 获取某年某周的时间跨度

public static Map getWeekRangeMap(int year, int week) {
        Map timeMap = new HashMap();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置星期一为一周开始的第一天
        calendar.setMinimalDaysInFirstWeek(4);//可以不用设置
        int weekYear = calendar.get(Calendar.YEAR);//获得当前的年
        calendar.setWeekDate(weekYear, week, 2);//获得指定年的第几周的开始日期
        Date time = calendar.getTime();
        String startTime = new SimpleDateFormat("yyyy-MM-dd").format(time);
        timeMap.put("startDate", startTime + " 00:00:00");
        calendar.setWeekDate(weekYear, week, 1);//获得指定年的第几周的结束日期
        time = calendar.getTime();
        String endTime = new SimpleDateFormat("yyyy-MM-dd").format(time);
        timeMap.put("endDate", endTime + " 23:59:59");
        timeMap.put("year",year);
        timeMap.put("number",week);
        return timeMap;
    }

6. 获取某年有多少周

public static int getYearWeekConut(int year) {
        int week = 52;
        try {
            Map timeMap = getWeekRangeMap(year, 53);
            if (!CollectionUtils.isEmpty(timeMap)) {
                String startTime = timeMap.get("startTime").toString();
                if (startTime.substring(0, 4).equals(year + "")) { //判断年度是否相符,如果相符说明有53个周。
                    week = 53;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return week;
    }

7. 获取某年所有周的时间跨度

public static Map> getYearWeekMap(int year) {
        int weeks = getYearWeekConut(year);
        Map> yearWeekMap = new HashMap>();
        for (int i = 1; i <= weeks; i++) {
            Map timeMap = getWeekRangeMap(year, i);
            yearWeekMap.put(i + "", timeMap);
        }
        return yearWeekMap;
    }

 8. 获取进七周时间,每一周的开始时间-结束时间(跨年)

public static List> getSevenWeek(){
        List> listMap = new ArrayList<>();
        Map map = new HashMap<>();
        // 获取近七周
        // 1. 获取当前是第几周
        Calendar calendar = Calendar.getInstance();
        int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
        // 2 如果当前周数小于7,需要跨上一个年度
        if(7 > weekOfYear){
            // 2.1 获取上一年度
            int yearTop = calendar.get(Calendar.YEAR) - 1;
            // 2.2 获取上一年度总共几周
            Integer weekCount = DateUtil.getYearWeekConut(yearTop);
            System.out.println(DateUtil.getYearWeekConut(yearTop));
            // 2.3 需要问上一年拿末尾几周的数据
            int count = 7 - weekOfYear + 1;
            // 2.3.1 跨 6,5,4,3,2,1
            switch (count){
                case 6 :
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount - 5));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount - 4));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount - 3));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount - 2));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount - 1));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,1));
                    break;
                case 5 :
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount - 4));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount - 3));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount - 2));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount - 1));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,1));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,2));
                    break;
                case 4 :
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount - 3));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount - 2));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount - 1));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,1));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,2));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,3));
                    break;
                case 3 :
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount - 2));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount - 1));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,1));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,2));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,3));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,4));
                    break;
                case 2 :
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount - 1));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,1));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,2));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,3));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,4));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,5));
                    break;
                case 1 :
                    listMap.add(DateUtil.getWeekRangeMap(yearTop,weekCount));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,1));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,2));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,3));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,4));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,5));
                    listMap.add(DateUtil.getWeekRangeMap(yearTop+1,6));
                    break;
                default:
                    break;
            }
        }
        return listMap;
    }

9. 获取近七月的时间,每月开始时间-结束时间(跨年)

public static List> getSevenMonth(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List> listMap = new ArrayList<>();
        //获取前月的第一天
        for (int i = 0; i >-7; i--) {
            Map map = new HashMap<>();
            map.clear();
            Calendar cal_1 = Calendar.getInstance();//获取当前日期
            cal_1.add(Calendar.MONTH, i);
            cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
            String firstDay = format.format(cal_1.getTime());
            map.put("startDate",firstDay);
            //获取前月的最后一天
            Calendar cale = Calendar.getInstance();
            cale.add(Calendar.MONTH, i+1);//设置为1号,当前日期既为本月第一天
            cale.set(Calendar.DAY_OF_MONTH, 0);//设置为1号,当前日期既为本月第一天
            String lastDay = format.format(cale.getTime());
            map.put("endDate",lastDay);
            map.put("year",lastDay.substring(0,7));
            listMap.add(map);
        }
        return listMap;
    }

10.获取近4个季度的时间,每个季度的开始时间-结束时间(跨年)

public static List> getFourQuarter1() {
        List> listMap = new ArrayList<>();
        Map map = new HashMap<>();
        Date now = new Date();
        for (int i = 0; i < 4; i++) {
            map.clear();
//            System.out.println(getYear(now) + "Q" + (getMM(now) + 2) / 3);
            String aa = getYear(now) + "Q" + (getMM(now) + 2) / 3;
            listMap.add(getFourQuarter2(aa.substring(0, 4),aa.substring(5,6)));
            now = monthAddNum(now, -3);
        }
        return listMap;
    }

    
    public static Map getFourQuarter2(String year,String num){
        Map map = new HashMap<>();
        switch (num) {
            case "1": // 本年到现在经过了一个季度,在加上前4个季度
                map.clear();
                map.put("startDate",year + "-01-01" + " 00:00:00");
                map.put("endDate",year + "-03-31" + " 23:59:59");
                map.put("quarter",year + "-1");
                break;
            case "2": // 本年到现在经过了二个季度,在加上前三个季度
                map.clear();
                map.put("startDate",year + "-04-01" + " 00:00:00");
                map.put("endDate",year + "-06-30" + " 23:59:59");
                map.put("quarter",year + "-2");
                break;
            case "3":// 本年到现在经过了三个季度,在加上前二个季度
                map.clear();
                map.put("startDate",year + "-07-01" + " 00:00:00");
                map.put("endDate",year + "-09-30" + " 23:59:59");
                map.put("quarter",year + "-3");
                break;
            case "4":// 本年到现在经过了四个季度,在加上前一个季度
                map.clear();
                map.put("startDate",year + "-10-01" + " 00:00:00");
                map.put("endDate",year + "-12-31" + " 23:59:59");
                map.put("quarter",year + "-4");
                break;
        }
        return map;
    }

11. 获取日期所在的月份

public static int getMM(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;

    }

12. 获取日期所在的年

public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);

    }

    public static Date monthAddNum(Date time, Integer num){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.MONTH, num);
        Date newTime = calendar.getTime();
        return newTime;
    }

13. 获取当天时间

    public static String getDate(){
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
        return sdf.format(new Date());
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5708421.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存