java根据传入的时间范围转换输出季度格式

最近项目有个需求就是根据传入的时间转换成季度查询数据,就是三个月查一次。下面时根据传入的时间进行季度输出。

import java.util.ArrayList;
import java.util.List;

/**
 * @author dingwl
 * @date 2019/10/23 16:24
 */
public class aa {

    public static String year(int year,int month){   //获取当前日期的季度
        String jd;
        if(month>=1&&month<=3){
            jd=year+"年"+"第一季度";
            return jd;
        }
        else if(month>=4&&month<=6){
            jd=year+"年"+"第二季度";
            return jd;
        }
        else  if(month>=7&&month<=9){
            jd=year+"年"+"第三季度";
            return jd;
        }
        else{
            jd=year+"年"+"第四季度";
            return jd;
        }
    }
    public static void main(String[] args) {
        int years=0, months=0 ,yeare=0,monthe=0;
        String[] sts = {};
        List<String> list = new ArrayList<>();
        list.add("2018-01");  //开始日期只能为 1,4,7,10
        list.add("2019-03");  //结束日期只能为 3,6,9,12
        for(int i=0;i<list.size();i++){
            if(i==0){        //获取第一个日期并根据“-”进行切割
                String str = list.get(i);
                sts = str.split("-");  //切割
                for(int j=0;j<sts.length;j++){
                    if(j==0){
                        years =Integer.parseInt(sts[j]); // 把string类型转换成int类型
                    }  else {
                        months = Integer.parseInt(sts[j]);
                    }
                }
            }else {
                String strs = list.get(i);
                sts = strs.split("-");
                for(int j=0;j<sts.length;j++){
                    if(j==0){
                        yeare = Integer.parseInt(sts[j]);
                    }  else {
                        monthe = Integer.parseInt(sts[j]);
                    }
                }
            }
        }
        int monthend=0;
        int yue = (yeare-years)*12+(monthe-months)+1; //计算有多少个月份
        for(int i = 0 ;i < yue/3;i++){   //计算有多少个季度
            aa.year(years,months);//years months 根据这个两个算季度
            System.out.println(aa.year(years,months));
            // 这里就是真实项目里面接口调用传入(years,months)(years,monthend)
                monthend = months+2; //每个季度结尾为开始+2
            //传入当前的(years ,months) 和 (years,monthee)
            if(monthend == 12){  //当结束月份为12 时就给开始月份赋值1 并且年份加1
                months = 1;
                years = years+1;
            }else {
                months+=3;  //给开始月份+3
            }

        }


    }
}

结果如下所示
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值