java获取不同时间段

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 
 * @ClassName: DemoText
 * @author: Mr.Li
 * @create: 2020-04-16 16:16
 **/
public class DemoText {
    public static void main(String[] args) throws ParseException {
                 // TODO Auto-generated method stub
                 String begintTime = "2019-03-04";
                 String endTime =  "2019-03-28";
        List<String> daysStr = findDaysStr(begintTime, endTime);

        System.out.println(daysStr.size());
        System.out.println("daysStr = " + daysStr);
        for(String days: daysStr){
                         System.out.println(days);
                     }
             }

             public static List<String> findDaysStr(String begintTime, String endTime) throws ParseException {
                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");


                 Date  dBegin = sdf.parse(begintTime);
                 Date   dEnd = sdf.parse(endTime);

                 List<String> daysStrList = new ArrayList<String>();
                 daysStrList.add(sdf.format(dBegin));
                 Calendar calBegin = Calendar.getInstance();
                 calBegin.setTime(dBegin);
                 Calendar calEnd = Calendar.getInstance();
                 calEnd.setTime(dEnd);
                 while (dEnd.after(calBegin.getTime())) {
                         calBegin.add(Calendar.DAY_OF_MONTH, 1);
                         String dayStr = sdf.format(calBegin.getTime());
                         daysStrList.add(dayStr);
                     }
                return daysStrList;
            }
			
	     public static List<String> getRangeSet(String beginDate,String endDate){
        /*      Date1.after(Date2),当Date1大于Date2时,返回TRUE,当小于等于时,返回false;
          Date1.before(Date2),当Date1小于Date2时,返回TRUE,当大于等于时,返回false;
          如果业务数据存在相等的时候,而且相等时也需要做相应的业务判断或处理时,你需要使用:!Date1.after(Date2);*/
        List<String> rangeSet =null;
        SimpleDateFormat sdf = null;
        Date begin_date = null;
        Date end_date = null;
        rangeSet = new java.util.ArrayList<String>();
        sdf = new SimpleDateFormat("yyyy-MM");
        try {
            begin_date = sdf.parse(beginDate);//定义起始日期
            end_date = sdf.parse(endDate);//定义结束日期
        } catch (ParseException e) {
            System.out.println("时间转化异常,请检查你的时间格式是否为yyyy-MM或yyyy-MM-dd");
        }
        Calendar dd = Calendar.getInstance();//定义日期实例
        dd.setTime(begin_date);//设置日期起始时间
        while(!dd.getTime().after(end_date)){//判断是否到结束日期
            rangeSet.add(sdf.format(dd.getTime()));
            dd.add(Calendar.MONTH, 1);//进行当前日期月份加1
        }
        return rangeSet;
    }
/**
     *根据时间范围获得季度集
     * @return
     */
    public static List<String> getRangeSet_Q(String beginDate,String endDate){
        /*      Date1.after(Date2),当Date1大于Date2时,返回TRUE,当小于等于时,返回false;
          Date1.before(Date2),当Date1小于Date2时,返回TRUE,当大于等于时,返回false;
          如果业务数据存在相等的时候,而且相等时也需要做相应的业务判断或处理时,你需要使用:!Date1.after(Date2);*/
        List<String> rangeSet =null;
        SimpleDateFormat sdf = null;
        Date begin_date = null;
        Date end_date = null;
        String[] numStr =null;
        String Q=null;
        rangeSet = new java.util.ArrayList<String>();
        sdf = new SimpleDateFormat("yyyy-MM");
        try {
            begin_date = sdf.parse(beginDate);//定义起始日期
            end_date = sdf.parse(endDate);//定义结束日期
        } catch (ParseException e) {
            System.out.println("时间转化异常,请检查你的时间格式是否为yyyy-MM或yyyy-MM-dd");
        }
        Calendar dd = Calendar.getInstance();//定义日期实例
        dd.setTime(begin_date);//设置日期起始时间
        while(!dd.getTime().after(end_date)){//判断是否到结束日期
            numStr=  sdf.format(dd.getTime()).split("-",0);
            Q = getQuarter(Integer.valueOf(numStr[1]))+"";
            System.out.println(numStr[0].toString()+"年"+numStr[1].toString()+"月"+"为"+numStr[0].toString()+"年第"+Q+"季");
            rangeSet.add(Q);
            dd.add(Calendar.MONTH, 1);//进行当前日期月份加1
        }
        return rangeSet;
    }
  /**
     * 根据月获得季度
     * @param month  月
     * @return  季度
     */
    private static int getQuarter(int month) {
        if (month == 1 || month == 2 || month == 3) {
            return 1;
        } else if (month == 4 || month == 5 || month == 6) {
            return 2;
        } else if (month == 7 || month == 8 || month == 9) {
            return 3;
        } else {
            return 4;
        }


    }
	
//查询时间段中的每一年
public static List<String> findYearStr(String begintTime, String endTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");


        Date  dBegin = sdf.parse(begintTime);
        Date   dEnd = sdf.parse(endTime);

        List<String> daysStrList = new ArrayList<String>();
        daysStrList.add(sdf.format(dBegin));
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(dEnd);
        while (dEnd.after(calBegin.getTime())) {
            calBegin.add(Calendar.YEAR, 1);
            String dayStr = sdf.format(calBegin.getTime());
            daysStrList.add(dayStr);
        }
        return daysStrList;
    }	
    /**
     * 获取过去7天内的日期数组
     * @return  日期数组
     */
    public static ArrayList<String> pastDay(String time){
        ArrayList<String> pastDaysList = new ArrayList<>();
        try {
            //我这里传来的时间是个string类型的,所以要先转为date类型的。
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(time);
            for (int i = 6; i >= 0; i--) {
                pastDaysList.add(getPastDate(i,date));
            }
        }catch (ParseException e){
            e.printStackTrace();
        }
        return pastDaysList;
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past,Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - past);
        Date today = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String result = sdf.format(today);
        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值