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 自己随便写的解析日期的类,大家可以自行百度就行。
第一行主要判断是否是合法日期区间。
还有另外一种实现方式,就是列举法,先把每年的季度列举出来,做匹配(太懒,不想写太多代码)。
转载请声明