场景:一个准确来说,我就听过名字,内部代码没有接触过的项目。应该是属于已经交付的,出问题了。大致是这么个意思,有一个关于时间判断来金钱收益。也就是周末没有收益,需要工作日才有收益。分一天收益,三天收益,七天收益。临近元旦,一天可以点进去,三天可以点进,7天就崩溃了。对应问题是报空指针。
由于一开始这个属于微信小程序,需要甲方提供测试环境。原先认为是小问题。就让给日志,报空指针不是有手就行吗。结果就给一个提示就说空指针。具体的堆栈没得了,应为前期小程序出问题比较多,所以就让对应堆栈答应去掉,就答应一个提示内容。也说了对应交付了,环境也没得。直接去撸已经离职同事的源码。好家伙。
这些都不是重点,就当了解一下知道骚操作还能这么玩的。大致是把判断时间的年月日,以及对应的是否是周末数据放在一个treemap里面,这个treemap放在redis中。问题都不大,大的问题是,就tm只放了一年。没错,从2020.01.01-2020.12.31。真就一年之期已到,这个程序就跑不动了。下面还有甲方的甲方提供的判断是否是周末的url连接,也是很棒的,2021年的数据没得。没办法就自己动手了。
本来对应改动越小,锅越小的原则。我对应写了时间的枚举类,写死,就让他们自己玩去吧。甲方不同意了,说要灵活变动。没办法,那么灵活变通。
这里主要是2个需要灵活变通的点。
1.获取某二个时间点之间的连续年月/年月日。
2.判断这个年月日对应是否是周末。
原先项目本身的时间工具类不支持。那么之间手动写。
具体代码如下
/** 2020-12-31 获取某个连续时间的年月 传参可以是 年份 或者年-月 或者年-月-日
获取连续的年月 / 年月日都可以 年份也支持 这个实现方法也可以用于对应连续时间获取,不过最好设置时间区间,免得无休止的调用
*/
//这个就是方法
public static List<String> getTimeList(String startDate, String endxDate){
SimpleDateFormat sdf ;
int calendarType;
/** 这里说明一下 对应格式可以是yyyy-MM-dd 不过对应case 10
这里是进行string分类,按照长度来划分用什么转换格式来进行时间转换
*/
switch (startDate.length()){
case 8:
sdf = new SimpleDateFormat("yyyyMMdd");
calendarType = Calendar.DATE;
break;
case 6:
sdf = new SimpleDateFormat("yyyyMM");
calendarType = Calendar.MONTH;
break;
case 4:
sdf = new SimpleDateFormat("yyyy");
calendarType = Calendar.YEAR;
break;
default:
return null;
}
List<String> result = new ArrayList<>();
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
try {
min.setTime(sdf.parse(startDate));
min.add(calendarType, 0);
max.setTime(sdf.parse(endxDate));
max.add(calendarType, 1);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar curr = min;
while (curr.before(max)) {
/** 这个是原先yyyy-MM-dd 进行去- 来达到实现之间的目的 */
result.add(sdf.format(min.getTime()).replace("-",""));
curr.add(calendarType, 1);
}
return result;
}
/** 这个是按照之间需要格式改写的。大意是获取某个时间 对应传参为年月 然后给对应当月最小 也就是拼接01 跟最大 获取当月最大天数 。获取到这二个时间之间的连续天数。然后对应天数对应的星期几改变,已知 周六跟周日是 N 12345是Y 所以直接改百度比较普遍的星期几的对应string[] 直接给安排了。获取我们需要的数据。
额外提一句,这里这么处理是因为我的redis存放值是 key+ 年月 。对应数据是获取年月的一个月的周末数据。用来数据处理,就很方便。
*/
public static TreeMap<String, String> getdateToWeek(String yearMonth) throws ParseException {
SimpleDateFormat sdf0 = new SimpleDateFormat("yyyyMM");
Calendar cal = Calendar.getInstance();
Date dd = sdf0.parse(yearMonth);
cal.setTime(dd);
int cc=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
String startDate = yearMonth.substring(0,4)+"-"+yearMonth.substring(4,6)+"-01";
String endxDate = yearMonth.substring(0,4)+"-"+yearMonth.substring(4,6)+"-" + cc;
SimpleDateFormat sdf ;
int calendarType;
switch (startDate.length()){
case 10:
sdf = new SimpleDateFormat("yyyy-MM-dd");
calendarType = Calendar.DATE;
break;
case 7:
sdf = new SimpleDateFormat("yyyy-MM");
calendarType = Calendar.MONTH;
break;
case 4:
sdf = new SimpleDateFormat("yyyy");
calendarType = Calendar.YEAR;
break;
default:
return null;
}
List<String> resultDate = new ArrayList<>();
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
try {
min.setTime(sdf.parse(startDate));
min.add(calendarType, 0);
max.setTime(sdf.parse(endxDate));
max.add(calendarType, 1);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar curr = min;
while (curr.before(max)) {
resultDate.add(sdf.format(min.getTime()).replace("-",""));
curr.add(calendarType, 1);
}
/** 上面进行数据处理 */
TreeMap<String, String> map = new TreeMap<>();
for(String datetime: resultDate){
SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
String[] weekDays = { "N", "Y", "Y", "Y", "Y", "Y", "N" };
// Calendar cal = Calendar.getInstance(); // 获得一个日历
Date datet = null;
datet = (Date) f.parse(datetime);
cal.setTime(datet);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
if (w < 0){
w = 0;
}
map.put(datetime,weekDays[w]);
}
return map;
}
就一直加班,人很忙碌,但是个人觉得收获有限。所以元旦休息了二天,也想了很多事情。需要多看一些东西,去学习更多的,打开自己的面,才能收获更优秀的自己。至自己,愿2021不迷茫,不放弃,做一个自律的浮华。