日常开发问题记录

  1. StringButils.isBlank/isNotBlank:
    if(param==null||"".equals(param.trim()){....}
    if(!(param==null||"".equals(param.trim())){....}
    //使用isBlank/isNotBlank代替上面代码,更有可读性
    if (StringUtils.isBlank(chargeSchemeNew)) {....}
    if (StringUtils.isNotBlank(chargeSchemeNew)) {....}
  2. excel日期读取问题:使用Apache POI对excel数据进行导入处理时,在对日期数据的处理中,excel里面的日期格式是yyyy-MM-dd,比如“2020/9/30”,但poi处理为String类型后显示为“30-九月-2020”
    String  str ="30-九月-2020";
    Date date = new SimpleDateFormat("ddd-MMM-yyyy").parse(str);
    System.out.println(date);   
    //====结果====
    Wed Sep 30 00:00:00 CST 2020
    
    //====或直接获取excel数据再进行是否是日期的判断和处理
    public static String importByExcelForDate(Cell currentCell) {
        String currentCellValue = "";
        // 判断单元格数据是否是日期
        if ("yyyy/mm;@".equals(currentCell.getCellStyle().getDataFormatString())
                || "m/d/yy".equals(currentCell.getCellStyle().getDataFormatString())
                || "yy/m/d".equals(currentCell.getCellStyle().getDataFormatString())
                || "mm/dd/yy".equals(currentCell.getCellStyle().getDataFormatString())
                || "dd-mmm-yy".equals(currentCell.getCellStyle().getDataFormatString())
                || "yyyy/m/d".equals(currentCell.getCellStyle().getDataFormatString())) {
            if (DateUtil.isCellDateFormatted(currentCell)) {
                // 用于转化为日期格式
                Date d = currentCell.getDateCellValue();
                DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
                currentCellValue = formater.format(d);
            }
        } else {
            // 不是日期原值返回
            currentCellValue = currentCell.toString();
        }
        return currentCellValue;
    }
  3. excel数值读取问题:在对excel数据是数值比如“50"进行获取然后toString后,转换为"50.0",开始是进行String.substring(0,cellData.length()-2)截取,后面添加currentRow.getCell(j).setCellType(HSSFCell.CELL_TYPE_STRING)就可以把数据直接处理成"50"
    public static Map analyzeExcel(MultipartFile file, Integer sheetIndex, Integer startRow) {
        List dataList = new LinkedList<>();
        Workbook wb = ExcelUtil.createWorkBook(file);
        if (wb != null) {
            Sheet sheet = wb.getSheetAt(sheetIndex);
            //一共有多少行
            int maxRownum = sheet.getPhysicalNumberOfRows();
            Row currentRow = sheet.getRow(startRow);
            //一共有多少列
            int maxColnum = currentRow.getPhysicalNumberOfCells();
            for (int i = startRow; i < maxRownum ; i ++) {
                currentRow = sheet.getRow(i);
                AppointmentListVo vo=new AppointmentListVo();
                if (currentRow != null) {
                    String cellData;
                    for (int j = 0; j < maxColnum; j++) {
                        //
                        currentRow.getCell(j).setCellType(HSSFCell.CELL_TYPE_STRING);
                        cellData = importByExcelForDate(currentRow.getCell(j));
                        if (j==0){
                            vo.setShortFirmName(cellData);
                        }else if(j==1){
                            vo.setTrainClassName(cellData);
                        } else if (j==2){
                            vo.setMacTypeName(cellData);
                        } else if (j==3){
                            vo.setDisplayChs(cellData);
                        }else if (j==4){
                            vo.setBeginTime(cellData);
                        }else if (j==5){
                            vo.setEndTime(cellData);
                        }else if(j==6){
                            //cellData =cellData.substring(0,cellData.length()-2);
                            vo.setTotalTrainPersons(Integer.parseInt(cellData));
                        }
                    }
                    dataList.add(vo);
                } else {
                    break;
                }
            }
        }
        Map map = new HashMap<>();
        map.put("dataList",dataList);
        return map;
    }
  4. Dubbo消费者生产者先后启动问题:如果消费者模块先启动再启动生产者模块,会导致服务调用报空指针异常
    //解决消费者先启动,生产者后启动导致消费者为null的问题
    @Configuration
    public class DubboConfig {
        /**
         * 消费者配置不主动监督zookeeper服务
         *
         * @return
         */
        @Bean
        public ConsumerConfig consumerConfig() {
            ConsumerConfig consumerConfig = new ConsumerConfig();
            consumerConfig.setCheck(false);
            consumerConfig.setTimeout(40000);
            return consumerConfig;
        }
    } 
  5. MyBatis的xml里面写SQL,对”<“要使用”<“代替:
      

     

你可能感兴趣的:(日常开发问题记录)