标签:出现 ring turn ati 遇到 ast 初始 tin 输入
遇到个需求就是计算过去的6个月是哪几个月 开始随便从网上找了个,
public static String[] getLastMonths(Calendar theCal) { String[] lastMonths = new String[6]; Calendar cal = theCal;//Calendar.getInstance(); cal.set(Calendar.MONTH, cal.MONTH); //要先+1,才能把本月的算进去 for(int i=0; i<6; i++){ cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1); //逐次往前推1个月 String t = cal.get(Calendar.YEAR)+ "-" + fillZero((cal.get(Calendar.MONTH)+1), 2); lastMonths[5-i] = t.replace("-", ""); } return lastMonths; }
但是运行时候出现了连续的问题,输入为201908时,会输出 201907 201907 201906 201905 201904 201903
或者 201907 201906 201905 201904 201904 201903
后来又写了个:
正确的月份-1
public static String[] getLastMonths(Calendar theCal) { String[] lastMonths = new String[6]; Calendar cal = theCal;//Calendar.getInstance(); cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)); //要先+1,才能把本月的算进去 for(int i=0; i<6; i++){ cal.add(Calendar.MONTH, -1); //逐次往前推1个月 String t = cal.get(Calendar.YEAR)+ "-" + fillZero((cal.get(Calendar.MONTH)+1), 2); lastMonths[5-i] = t.replace("-", ""); } return lastMonths; }
怀疑是cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1);的初始化有问题,老是慢一拍,感觉有的时候这个-1并不能正常执行,就开始向下执行了,
所以,还是用这个正常的吧, cal.add(Calendar.MONTH, -1); //逐次往前推1个月
标签:出现 ring turn ati 遇到 ast 初始 tin 输入
原文地址:https://www.cnblogs.com/1394htw/p/11407167.html