zno2

java 给定时间范围计算 年、季、月、日

 

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import cn.xs.ambi.bas.util.DateUtils;
import lombok.CustomLog;

/**
 * @author witas
 *
 */
@CustomLog
public class StatUtils {

    /**
     * @param dayStart
     * @param dayEnd
     * @param xtype 1年,2季,3月,4日
     * @return
     */
    public static List<String> timeUnitAll(Date dayStart, Date dayEnd, Integer xtype) {
        List<String> result = new ArrayList<>();
        if (dayStart == null || dayEnd == null || dayStart.compareTo(dayEnd) > 0) {
            log.info("时间范围取值错误");
            return result;
        }
        if (xtype == null) {
            log.info("时间粒度不能为空");
            return result;
        }
        Calendar start = Calendar.getInstance();
        start.setTime(dayStart);
        Calendar end = Calendar.getInstance();
        end.setTime(dayEnd);
        if (xtype == 1) {
            int startY = start.get(Calendar.YEAR);
            int endY = end.get(Calendar.YEAR);
            for (int i = startY; i <= endY; i++) {
                result.add(i + "");
            }
        } else if (xtype == 2) {
            Set<String> set = new TreeSet<>();
            DateTimeFormatter p = DateTimeFormat.forPattern("yyyy-MM-dd");
            String endFlag = DateUtils.format(dayEnd, p);
            String s = DateUtils.format(start.getTime(), p);
            while (s.compareTo(endFlag) <= 0) {
                set.add(getQuarter(s));
                start.add(Calendar.DAY_OF_YEAR, 1);
                s = DateUtils.format(start.getTime(), p);
            }
            result.addAll(set);
        } else if (xtype == 3) {
            DateTimeFormatter p = DateTimeFormat.forPattern("yyyy-MM");
            String endFlag = DateUtils.format(dayEnd, p);
            String s = DateUtils.format(start.getTime(), p);
            while (s.compareTo(endFlag) <= 0) {
                result.add(s);
                start.add(Calendar.MONTH, 1);
                s = DateUtils.format(start.getTime(), p);
            }
        } else {
            DateTimeFormatter p = DateTimeFormat.forPattern("yyyy-MM-dd");
            String endFlag = DateUtils.format(dayEnd, p);
            String s = DateUtils.format(start.getTime(), p);
            while (s.compareTo(endFlag) <= 0) {
                result.add(s);
                start.add(Calendar.DAY_OF_YEAR, 1);
                s = DateUtils.format(start.getTime(), p);
            }
        }
        return result;
    }
    
    
    /**
     * 获取季度 
     * @param t yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getQuarter(String t) {
        Integer m = Integer.valueOf(t.substring(5, 7));
        int q = m / 3;
        if (m % 3 != 0) {
            q++;
        }
        String n = "";
        if (q == 1) {
            n = "1";
        } else if (q == 2) {
            n = "2";
        } else if (q == 3) {
            n = "3";
        } else if (q == 4) {
            n = "4";
        }
        return t.substring(0, 4) + "年第" + n + "季度";
    }
}

 

测试:

import java.util.Date;

import org.junit.Test;

import cn.xs.ambi.bas.util.DateUtils;

public class StatUtilsTest {

    @Test
    public void testTimeUnitAll() {
        Date dayStart = DateUtils.toDateNull("2021-04-01 00:00:00", DateUtils.TIME_3);
        Date dayEnd = DateUtils.toDateNull("2022-07-28 00:00:00", DateUtils.TIME_3);
        System.out.println(StatUtils.timeUnitAll(dayStart, dayEnd, 1));
        System.out.println(StatUtils.timeUnitAll(dayStart, dayEnd, 2));
        System.out.println(StatUtils.timeUnitAll(dayStart, dayEnd, 3));
        System.out.println(StatUtils.timeUnitAll(dayStart, dayEnd, 4));
    }

    @Test
    public void testGetQuarter() {
        assert(StatUtils.getQuarter("2022-01-28").equals("2022年第1季度"));
        assert(StatUtils.getQuarter("2022-04-28").equals("2022年第2季度"));
        assert(StatUtils.getQuarter("2022-07-28").equals("2022年第3季度"));
        assert(StatUtils.getQuarter("2022-12-28").equals("2022年第4季度"));
    }

}

 

结果:

[2021, 2022]
[2021年第2季度, 2021年第3季度, 2021年第4季度, 2022年第1季度, 2022年第2季度, 2022年第3季度]
[2021-04, 2021-05, 2021-06, 2021-07, 2021-08, 2021-09, 2021-10, 2021-11, 2021-12, 2022-01, 2022-02, 2022-03, 2022-04, 2022-05, 2022-06, 2022-07]
[2021-04-01, 2021-04-02, 2021-04-03, 2021-04-04, 2021-04-05, 2021-04-06, 2021-04-07, 2021-04-08, 2021-04-09, 2021-04-10, 2021-04-11, 2021-04-12, 2021-04-13, 2021-04-14, 2021-04-15, 2021-04-16, 2021-04-17, 2021-04-18, 2021-04-19, 2021-04-20, 2021-04-21, 2021-04-22, 2021-04-23, 2021-04-24, 2021-04-25, 2021-04-26, 2021-04-27, 2021-04-28, 2021-04-29, 2021-04-30, 2021-05-01, 2021-05-02, 2021-05-03, 2021-05-04, 2021-05-05, 2021-05-06, 2021-05-07, 2021-05-08, 2021-05-09, 2021-05-10, 2021-05-11, 2021-05-12, 2021-05-13, 2021-05-14, 2021-05-15, 2021-05-16, 2021-05-17, 2021-05-18, 2021-05-19, 2021-05-20, 2021-05-21, 2021-05-22, 2021-05-23, 2021-05-24, 2021-05-25, 2021-05-26, 2021-05-27, 2021-05-28, 2021-05-29, 2021-05-30, 2021-05-31, 2021-06-01, 2021-06-02, 2021-06-03, 2021-06-04, 2021-06-05, 2021-06-06, 2021-06-07, 2021-06-08, 2021-06-09, 2021-06-10, 2021-06-11, 2021-06-12, 2021-06-13, 2021-06-14, 2021-06-15, 2021-06-16, 2021-06-17, 2021-06-18, 2021-06-19, 2021-06-20, 2021-06-21, 2021-06-22, 2021-06-23, 2021-06-24, 2021-06-25, 2021-06-26, 2021-06-27, 2021-06-28, 2021-06-29, 2021-06-30, 2021-07-01, 2021-07-02, 2021-07-03, 2021-07-04, 2021-07-05, 2021-07-06, 2021-07-07, 2021-07-08, 2021-07-09, 2021-07-10, 2021-07-11, 2021-07-12, 2021-07-13, 2021-07-14, 2021-07-15, 2021-07-16, 2021-07-17, 2021-07-18, 2021-07-19, 2021-07-20, 2021-07-21, 2021-07-22, 2021-07-23, 2021-07-24, 2021-07-25, 2021-07-26, 2021-07-27, 2021-07-28, 2021-07-29, 2021-07-30, 2021-07-31, 2021-08-01, 2021-08-02, 2021-08-03, 2021-08-04, 2021-08-05, 2021-08-06, 2021-08-07, 2021-08-08, 2021-08-09, 2021-08-10, 2021-08-11, 2021-08-12, 2021-08-13, 2021-08-14, 2021-08-15, 2021-08-16, 2021-08-17, 2021-08-18, 2021-08-19, 2021-08-20, 2021-08-21, 2021-08-22, 2021-08-23, 2021-08-24, 2021-08-25, 2021-08-26, 2021-08-27, 2021-08-28, 2021-08-29, 2021-08-30, 2021-08-31, 2021-09-01, 2021-09-02, 2021-09-03, 2021-09-04, 2021-09-05, 2021-09-06, 2021-09-07, 2021-09-08, 2021-09-09, 2021-09-10, 2021-09-11, 2021-09-12, 2021-09-13, 2021-09-14, 2021-09-15, 2021-09-16, 2021-09-17, 2021-09-18, 2021-09-19, 2021-09-20, 2021-09-21, 2021-09-22, 2021-09-23, 2021-09-24, 2021-09-25, 2021-09-26, 2021-09-27, 2021-09-28, 2021-09-29, 2021-09-30, 2021-10-01, 2021-10-02, 2021-10-03, 2021-10-04, 2021-10-05, 2021-10-06, 2021-10-07, 2021-10-08, 2021-10-09, 2021-10-10, 2021-10-11, 2021-10-12, 2021-10-13, 2021-10-14, 2021-10-15, 2021-10-16, 2021-10-17, 2021-10-18, 2021-10-19, 2021-10-20, 2021-10-21, 2021-10-22, 2021-10-23, 2021-10-24, 2021-10-25, 2021-10-26, 2021-10-27, 2021-10-28, 2021-10-29, 2021-10-30, 2021-10-31, 2021-11-01, 2021-11-02, 2021-11-03, 2021-11-04, 2021-11-05, 2021-11-06, 2021-11-07, 2021-11-08, 2021-11-09, 2021-11-10, 2021-11-11, 2021-11-12, 2021-11-13, 2021-11-14, 2021-11-15, 2021-11-16, 2021-11-17, 2021-11-18, 2021-11-19, 2021-11-20, 2021-11-21, 2021-11-22, 2021-11-23, 2021-11-24, 2021-11-25, 2021-11-26, 2021-11-27, 2021-11-28, 2021-11-29, 2021-11-30, 2021-12-01, 2021-12-02, 2021-12-03, 2021-12-04, 2021-12-05, 2021-12-06, 2021-12-07, 2021-12-08, 2021-12-09, 2021-12-10, 2021-12-11, 2021-12-12, 2021-12-13, 2021-12-14, 2021-12-15, 2021-12-16, 2021-12-17, 2021-12-18, 2021-12-19, 2021-12-20, 2021-12-21, 2021-12-22, 2021-12-23, 2021-12-24, 2021-12-25, 2021-12-26, 2021-12-27, 2021-12-28, 2021-12-29, 2021-12-30, 2021-12-31, 2022-01-01, 2022-01-02, 2022-01-03, 2022-01-04, 2022-01-05, 2022-01-06, 2022-01-07, 2022-01-08, 2022-01-09, 2022-01-10, 2022-01-11, 2022-01-12, 2022-01-13, 2022-01-14, 2022-01-15, 2022-01-16, 2022-01-17, 2022-01-18, 2022-01-19, 2022-01-20, 2022-01-21, 2022-01-22, 2022-01-23, 2022-01-24, 2022-01-25, 2022-01-26, 2022-01-27, 2022-01-28, 2022-01-29, 2022-01-30, 2022-01-31, 2022-02-01, 2022-02-02, 2022-02-03, 2022-02-04, 2022-02-05, 2022-02-06, 2022-02-07, 2022-02-08, 2022-02-09, 2022-02-10, 2022-02-11, 2022-02-12, 2022-02-13, 2022-02-14, 2022-02-15, 2022-02-16, 2022-02-17, 2022-02-18, 2022-02-19, 2022-02-20, 2022-02-21, 2022-02-22, 2022-02-23, 2022-02-24, 2022-02-25, 2022-02-26, 2022-02-27, 2022-02-28, 2022-03-01, 2022-03-02, 2022-03-03, 2022-03-04, 2022-03-05, 2022-03-06, 2022-03-07, 2022-03-08, 2022-03-09, 2022-03-10, 2022-03-11, 2022-03-12, 2022-03-13, 2022-03-14, 2022-03-15, 2022-03-16, 2022-03-17, 2022-03-18, 2022-03-19, 2022-03-20, 2022-03-21, 2022-03-22, 2022-03-23, 2022-03-24, 2022-03-25, 2022-03-26, 2022-03-27, 2022-03-28, 2022-03-29, 2022-03-30, 2022-03-31, 2022-04-01, 2022-04-02, 2022-04-03, 2022-04-04, 2022-04-05, 2022-04-06, 2022-04-07, 2022-04-08, 2022-04-09, 2022-04-10, 2022-04-11, 2022-04-12, 2022-04-13, 2022-04-14, 2022-04-15, 2022-04-16, 2022-04-17, 2022-04-18, 2022-04-19, 2022-04-20, 2022-04-21, 2022-04-22, 2022-04-23, 2022-04-24, 2022-04-25, 2022-04-26, 2022-04-27, 2022-04-28, 2022-04-29, 2022-04-30, 2022-05-01, 2022-05-02, 2022-05-03, 2022-05-04, 2022-05-05, 2022-05-06, 2022-05-07, 2022-05-08, 2022-05-09, 2022-05-10, 2022-05-11, 2022-05-12, 2022-05-13, 2022-05-14, 2022-05-15, 2022-05-16, 2022-05-17, 2022-05-18, 2022-05-19, 2022-05-20, 2022-05-21, 2022-05-22, 2022-05-23, 2022-05-24, 2022-05-25, 2022-05-26, 2022-05-27, 2022-05-28, 2022-05-29, 2022-05-30, 2022-05-31, 2022-06-01, 2022-06-02, 2022-06-03, 2022-06-04, 2022-06-05, 2022-06-06, 2022-06-07, 2022-06-08, 2022-06-09, 2022-06-10, 2022-06-11, 2022-06-12, 2022-06-13, 2022-06-14, 2022-06-15, 2022-06-16, 2022-06-17, 2022-06-18, 2022-06-19, 2022-06-20, 2022-06-21, 2022-06-22, 2022-06-23, 2022-06-24, 2022-06-25, 2022-06-26, 2022-06-27, 2022-06-28, 2022-06-29, 2022-06-30, 2022-07-01, 2022-07-02, 2022-07-03, 2022-07-04, 2022-07-05, 2022-07-06, 2022-07-07, 2022-07-08, 2022-07-09, 2022-07-10, 2022-07-11, 2022-07-12, 2022-07-13, 2022-07-14, 2022-07-15, 2022-07-16, 2022-07-17, 2022-07-18, 2022-07-19, 2022-07-20, 2022-07-21, 2022-07-22, 2022-07-23, 2022-07-24, 2022-07-25, 2022-07-26, 2022-07-27, 2022-07-28]

 

posted on 2023-06-02 08:17  zno2  阅读(444)  评论(0编辑  收藏  举报

导航