返回任意两个yyyyMM的差值
获取任意两个yyyyMM的差值
1.给定两个任意的年月值,格式为yyyyMM,返回其中的差值.
eg:输入"202002"与"202106",返回.
202003
202004
202005
202006
202007
202008
202009
202010
202011
202012
202101
202102
202103
202104
202105
2.参考代码
没有推敲细节,肯定可以优化,仅参考.
package main.cn.yang37.algorithm;
import java.util.*;
import java.util.stream.Collectors;
/**
* @Description: 给定两个yyyyMM返回中间的月份差yyyyMM
* @Class: CalculateMonthDifference
* @Author: Yiang37
* @Date: 2021/2/4 22:34
* @Version: 1.0
*/
public class CalculateMonthDifference {
public static void main(String[] args) {
List<String> list = getDifferenceMonths("201809", "202011");
for (String s : list) {
System.out.println(s);
}
}
public static List<String> getDifferenceMonths(String time1, String time2) {
try {
//正则校验入参格式..
String bigStr;
String lowStr;
//判断年份大小
if (Integer.parseInt(time1) > Integer.parseInt(time2)) {
bigStr = time1;
lowStr = time2;
} else {
bigStr = time2;
lowStr = time1;
}
String bigYearStr1 = bigStr.substring(0, 4);
String bigMonthStr1 = bigStr.substring(4, 6);
String lowYearStr2 = lowStr.substring(0, 4);
String lowMonthStr2 = lowStr.substring(4, 6);
int bigYear = Integer.parseInt(bigYearStr1);
int bigMonth = Integer.parseInt(bigMonthStr1);
int lowYear = Integer.parseInt(lowYearStr2);
int lowMoth = Integer.parseInt(lowMonthStr2);
//月份差
int range = 0;
//数字型结果月份
int resMonth = 0;
//字符型结果月份
String resMonthStr = "";
//结果列表
List<String> resList = new LinkedList<>();
//同一年份
if (bigYear == lowYear) {
range = bigMonth - lowMoth;
for (int i = 1; i < range; i++) {
//差值月份
resMonth = lowMoth + i;
//小于10的月份要补0
resMonthStr = resMonth > 9 ? "" + resMonth : "0" + resMonth;
resList.add("" + lowYear + resMonthStr);
}
}//年份是连续的
else if (bigYear - lowYear == 1) {
//小年月份距离12月的位置
range = 12 - lowMoth;
//小年份到年底十二月
for (int i = 1; i <= range; i++) {
//差值月份
resMonth = lowMoth + i;
//小于10的月份要补0
resMonthStr = resMonth > 9 ? "" + resMonth : "0" + resMonth;
resList.add("" + lowYear + resMonthStr);
}
//大年份初到大年份当前
for (int i = 1; i < bigMonth; i++) {
resMonth = i;
resMonthStr = resMonth > 9 ? "" + resMonth : "0" + resMonth;
resList.add("" + bigYear + resMonthStr);
}
}
//年份是不连续的 2020 2025
else {
//计算中间整数倍的差值年份
int yearRange = bigYear - lowYear;
int tempYear = 0;
String tempYearStr = "";
String tempMonthStr = "";
//外层控制年份
for (int i = 1; i < yearRange; i++) {
tempYear = lowYear + i;
tempYearStr = "" + tempYear;
//内层控制年份
for (int j = 1; j <= 12; j++) {
tempMonthStr = j > 9 ? "" + j : "0" + j;
resList.add(tempYearStr + tempMonthStr);
}
}
//计算两边的差值月份 复用连续年份的算法
//小年月份距离12月的位置
range = 12 - lowMoth;
//小年份到年底十二月
for (int i = 1; i <= range; i++) {
//差值月份
resMonth = lowMoth + i;
//小于10的月份要补0
resMonthStr = resMonth > 9 ? "" + resMonth : "0" + resMonth;
resList.add("" + lowYear + resMonthStr);
}
//大年份初到大年份当前
for (int i = 1; i < bigMonth; i++) {
resMonth = i;
resMonthStr = resMonth > 9 ? "" + resMonth : "0" + resMonth;
resList.add("" + bigYear + resMonthStr);
}
}
//结果list由String转Integer
List<Integer> list = resList.stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
Collections.sort(list);
//排序后的list再转回去String 我竟然又转回去??
resList = list.stream().map(String::valueOf).collect(Collectors.toList());
return resList;
}catch (Exception e){
return new ArrayList<String>(){{
add("出错了");
}};
}
}
}