欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

时间工具类DataUtils.java,通过输入开始时间和截至时间,获取之间的日期

程序员文章站 2022-05-17 21:27:36
...
在进行数据统计时,常常需要通过接收2个参数,起始时间和结束时间,获取2个时间之间的日期。

例如:输入参数:2019-04-01,2019-04-10

需要获取2个时间之间的日期:即:[2019-04-01,2019-04-02,2019-04-03,2019-04-04,2019-04-05,2019-04-06,2019-04-07,2019-04-08,2019-04-09,2019-04-10]

本例子采用多种时间格式:分别为:日期、月份、季度、年份等4种维度。

详情如下:

package com.casic.ht304.cdgl.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author fanyanyan
 * @Title: DateUtils
 * @ProjectName testProject
 * @date 2019/4/23 15:16
 */
public class DateUtils {
    /**
     * 获取两个日期之间的日期,2019-04-01,2019-04-10
     *
     * @param startTime
     * @param endTime
     * @return 日期的List<String>集合
     */
    public List<Object> getBetweenDate(String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        // 声明保存日期集合
        List<Object> list = new ArrayList<>();
        try {
            // 转化成日期类型
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);
            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime() <= endDate.getTime()) {
                // 把日期添加到集合
                list.add(sdf.format(startDate));
                // 设置日期
                calendar.setTime(startDate);
                //把日期增加一天
                calendar.add(Calendar.DATE, 1);
                // 获取增加后的日期
                startDate = calendar.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return list;
    }

    /**
     * 获取两个月份日期之间的月份:2019-01,2019-04
     *
     * @param startTime
     * @param endTime
     * @return 月份日期的List<String>集合
     */
    public List<Object> getBetweenMonth(String startTime, String endTime) {
        List list = new ArrayList<Object>();
        String[] timeStart = startTime.split("-");
        String[] timeEnd = endTime.split("-");
        int yearStart = Integer.parseInt(timeStart[0]);
        int firstYear = yearStart;
        int yearEnd = Integer.parseInt(timeEnd[0]);
        int monthStart = Integer.parseInt(timeStart[1]);
        int firstMonth = yearStart;
        int monthEnd = Integer.parseInt(timeEnd[1]);
        while (yearStart <= yearEnd) {
            if (yearStart < yearEnd) {
                int i = monthStart;
                if (yearStart != firstYear){
                    i=1;
                }
                while (i <= 12) {
                    String date = "";
                    if ((i + "").length() == 1) {
                        date = yearStart + "" + "-0" + i + "";
                    } else {
                        date = yearStart + "" + "-" + i + "";
                    }
                    list.add(date);
                    i++;
                }
            }
            if (yearStart == yearEnd) {
                int i = 1;
                if (yearStart == firstYear){
                    i=monthStart;
                }
                while (i <= monthEnd) {
                    String date = "";
                    if ((i + "").length() == 1) {
                        date = yearStart + "" + "-0" + i + "";
                    } else {
                        date = yearStart + "" + "-" + i + "";
                    }
                    list.add(date);
                    i++;
                }
            }
            yearStart++;
        }
        return list;
    }

    /**
     * 获取两个年之间的所有的季度:2018,2019
     *
     * @param startTime
     * @param endTime
     * @return 季度日期的List<String>集合:[2018-1,2018-2,...,2019-3,2019-4]
     */
    public List<Object> getBetweenQuarter(String startTime, String endTime) {
        List list = new ArrayList<Object>();
        int yearStart = Integer.parseInt(startTime);
        int yearEnd = Integer.parseInt(endTime);
        while (yearStart <= yearEnd) {
            for (int i = 1; i <= 4; i++) {
                list.add(yearStart + "-" + i);
            }
            yearStart++;
        }

        return list;
    }

    /**
     * 获取两个年份之间的所有年份
     *
     * @param startTime
     * @param endTime
     * @return 返回年份List<String>:[2018,2019]
     */
    public List<Object> getBetweenYear(String startTime, String endTime) {
        List list = new ArrayList<>();
        int yearStart = Integer.parseInt(startTime);
        int yearEnd = Integer.parseInt(endTime);
        while (yearStart <= yearEnd) {
            list.add(yearStart);
            yearStart++;
        }
        return list;
    }

    public static void main(String[] args) {
        DateUtils du = new DateUtils();
//        System.out.println(du.getBetweenYear("2018", "2019"));
//        System.out.println(du.getBetweenQuarter("2018", "2019"));
        System.out.println(du.getBetweenMonth("2018-01", "2018-02"));
//        System.out.println(du.getBetweenDate("2019-04-01", "2019-04-22"));
    }
}

 

相关标签: java 日期