java 根据起始日期获取季度

public static void main(String[] args) {
		String s1 = "2015-01-09";
		String s2 = "2021-12-09";

		if (DateTools.parseDate(s2).after(DateTools.parseDate(s1))) {
			String y1 = s1.substring(0, 4);
			String y2 = s2.substring(0, 4);
			String m1 = s1.substring(5, 7);
			String m2 = s2.substring(5, 7);
			if (y1.equals(y2)) {// 当年
				Integer q = Integer.valueOf(m2) / 3 == 0 ? Integer.valueOf(m2) / 3 + 1 : Integer.valueOf(m2) / 3;
				Integer q1 = Integer.valueOf(m1) / 3 == 0 ? Integer.valueOf(m1) / 3 + 1 : Integer.valueOf(m1) / 3;
				for (int i = q1; i <= q; i++) {
					System.out.println(y1 + "/Q" + i);
				}
			} else if (Integer.valueOf(y2) > Integer.valueOf(y1)) {
				Integer s = Integer.valueOf(y2) - Integer.valueOf(y1);
				Integer q = Integer.valueOf(m2) / 3 == 0 ? Integer.valueOf(m2) / 3 + 1 : Integer.valueOf(m2) / 3;
				Integer q1 = Integer.valueOf(m1) / 3 == 0 ? Integer.valueOf(m1) / 3 + 1 : Integer.valueOf(m1) / 3;
				for (int j = 0; j <= s; j++) {
					Integer y = Integer.valueOf(y1) + j;
					for (int i = y == Integer.valueOf(y1).intValue() ? q1 : 1; i <= (q + j > 4 ? 4 : q + j); i++) {
						System.out.println(y + "/Q" + i);
					}
				}
			}

		} else {

			System.out.println("错误时间");
		}

	}

 

DateTools 自己随便写的解析日期的类,大家可以自行百度就行。

第一行主要判断是否是合法日期区间。

 

还有另外一种实现方式,就是列举法,先把每年的季度列举出来,做匹配(太懒,不想写太多代码)。

转载请声明

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值