使用LocalDate场景应用
应用场景:计算出2018-03-01到2018-06-25日之间每日交易金额,如果这这段时间内某天没有人任何交易,
那么用mysql按照日期分组是不完整的,缺失某几天数据,所以LocalDate派上用场了。
例如:某金融系统当需求设计出2018-05-01到2018-06-30日之间的交易时段(设置哪些时间段可以交易哪些时间段不可以易)
效果如下:
使用技巧:根据起始时间和结束时间,然后利用LocalDate方法计算出总天数,until(endTime, ChronoUnit.DAYS);
然后使用循环天数,每次循环起始时间加一天,使用方法,.plusDays(1);
代码效果如下:
List<ActiveMemberBean> buyList = new ArrayList<ActiveMemberBean>(); //买list
List<ActiveMemberBean> sellList = new ArrayList<ActiveMemberBean>(); //卖list
//设置开始和结束时间 LocalDate startTime = LocalDate.parse(financeBillParam.getStartTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd")); LocalDate endTime = LocalDate.parse(financeBillParam.getEndTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd")); //获取查询出的天数 int day = Integer.valueOf(Common.until(startTime,endTime) +"") + 1; for(int i = 0;i< day;i++){ //当前日期时间字符串 String startTimeString = startTime.toString(); ActiveMemberBean entity = new ActiveMemberBean(); entity.setDay(startTime.toString()); if(buyList != null && !buyList.isEmpty()){ List<ActiveMemberBean> list = buyList.stream().filter(o -> o.getDay().contains(startTimeString)).collect(Collectors.toList()); if(list != null && !list.isEmpty()){ entity.setBuyNumber(list.get(0).getNumber()); }else{ entity.setBuyNumber(0); } }else{ entity.setBuyNumber(0); } if(sellList != null && !sellList.isEmpty()){ List<ActiveMemberBean> list = sellList.stream().filter(o -> o.getDay().contains(startTimeString)).collect(Collectors.toList()); if(list != null && !list.isEmpty()){ entity.setSellerNumber(list.get(0).getNumber()); }else{ entity.setSellerNumber(0); } }else{ entity.setSellerNumber(0); } resultList.add(entity); startTime = startTime.plusDays(1); }
/** * 计算日期{@code startDate}与{@code endDate}的间隔天数 * * @param startDate * @param endDate * @return 间隔天数 */ public static long until(LocalDate startDate, LocalDate endDate){ return startDate.until(endDate, ChronoUnit.DAYS); }
重点介绍使用LocalDate计算出相差月数误区,比如2018-04-22日到2018-04-21日中,计算出每个月交易数据统计,
使用until(endTime, ChronoUnit.MONTHS); 算出的结果是0,
正确是使用方法是。从2018-04月开始循环,使用.plusMonths(1),直到2018-05结束时间相匹配,然后就break 结束
代码效果如下:
//第一步计算出所有的月份
//设置开始和结束时间--monthList是月份集合
LocalDate startTime = LocalDate.parse(withdeawParamBean.getStartTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd")); LocalDate endTime = LocalDate.parse(withdeawParamBean.getEndTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd")); DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM"); String endMonth = df.format(endTime); while(true){ String month = df.format(startTime); monthList.add(month); if(month.equals(endMonth)){ break; }else{ startTime = startTime.plusMonths(1); } } return resultList;
//第二步计算出所有的月份
根据月份循环出来跟数据源(交易数据等等)做匹配(把交易数据时间格式化成2018-05)
for(int i=0;i<月份数量;i++){
if(list[i] == 交易数据格式化后的数据){
做累计处理
}}
//数据遍历 if(monthList != null && !monthList.isEmpty()){ for(int i=0;i<monthList.size();i++){ BaseCountBean baseCountBean = new BaseCountBean(); baseCountBean.setDay(monthList.get(i)); Integer num = i; List<BaseCountBean> filterList = list.stream().filter(o -> o.getDay().contains(monthList.get(num).toString())).collect(Collectors.toList()); if(filterList != null && !filterList.isEmpty()){ BigDecimal amount = BigDecimal.ZERO; for(BaseCountBean entity:filterList){ amount = amount.add(entity.getAmount()); } baseCountBean.setAmount(amount); }else{ baseCountBean.setAmount(BigDecimal.ZERO); } resultList.add(baseCountBean); } }
List<String> monthList = new ArrayList<String>(); //月份集合