1. 情景:计算未来某时间点与当前时间的时间差(day,hour,minute),可以写成一个util工具类。
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //格式化时间 String now = simpleDateFormat.format(new Date()); //获取当前时间 try{ Date beginTime = simpleDateFormat.parse("2018-07-03 22:00:00"); Date nowTime = simpleDateFormat.parse(now); long day = (beginTime.getTime()-nowTime.getTime())/(60*60*1000*24); //天数差 long hour = (beginTime.getTime()-nowTime.getTime())/(60*60*1000); //小时差 long minute = (beginTime.getTime()-nowTime.getTime())/(60*1000); //分钟差 }catch(Exception e){ throw new RuntimeException(e); }
2. 情景:java定时任务,每天某时刻启动任务。
Runnable runnable = new Runnable() { @Override public void run() { System.out.println("需要执行的内容!"); } }; ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); service.scheduleAtFixedRate(runnable,initialDelay,24,TimeUnit.HOURS); //参数2第一次开始执行的时间,参数3第一次执行后多久开始执行第二次,参数4时间单位
3.情景:excel数据读取,使用poi的jar包,可以分别读取xls和xlsx两种excel格式。
import com.google.common.base.Joiner; import com.sankuai.meituan.drrsys.degarde.model.DSRecallResponse; import org.apache.commons.collections.CollectionUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.eclipse.jetty.util.StringUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.*; public class ReadExcel { private static Logger logger = LoggerFactory.getLogger(ReadExcel.class); private String excel = "excel"; private static final String EXTENSION_XLS = "xls"; private static final String EXTENSION_XLSX = "xlsx"; public static void main(String[] args){ ReadExcel readExcel = new ReadExcel(); readExcel.readExcel(); } public Map<String, List<DSRecallResponse>> readExcel(){ this.excel = getClass().getClassLoader().getResource(excel).getPath(); String path = excel + "/20180605query.xlsx"; Map<String, List<DSRecallResponse>> record = null; try{ record = readExcelFile(path); } catch (IOException e){ logger.error("Read excel fail!", e); } return record; } public Map<String, List<DSRecallResponse>> readExcelFile(String path) throws IOException { Map<String, List<DSRecallResponse>> record = null; InputStream is = new FileInputStream(path); try{ Workbook xssfWorkbook = null; if(path.endsWith(EXTENSION_XLS)){ xssfWorkbook = new HSSFWorkbook(is); }else if(path.endsWith(EXTENSION_XLSX)){ xssfWorkbook = new XSSFWorkbook(is); }else{ throw new IllegalArgumentException("file is not excel!"); } Sheet xssfSheet = xssfWorkbook.getSheetAt(0); if(xssfSheet == null){ return null; } record = new HashMap<>(); int lastRowNum = xssfSheet.getLastRowNum(); int startRowNum = 1; for(int rowNum = startRowNum; rowNum<=lastRowNum;rowNum++){ if (xssfSheet.getRow(rowNum) != null){ Row xssfRow = xssfSheet.getRow(rowNum); short lastCellNum = xssfRow.getLastCellNum(); if(lastCellNum >= 4){ String date = xssfRow.getCell(1).toString(); String requestId = xssfRow.getCell(4).toString(); if(date != null && StringUtil.isNotBlank(requestId)){ try{ String key = Joiner.on("_").skipNulls().join(date,requestId); List<DSRecallResponse> dsRecallResponseList = record.get(key); if(CollectionUtils.isEmpty(dsRecallResponseList)){ dsRecallResponseList = new ArrayList<>(); record.put(key, dsRecallResponseList); } DSRecallResponse dsRecallResponse = new DSRecallResponse(); dsRecallResponse.setData(xssfRow.getCell(1).toString()); dsRecallResponse.setRequestId(xssfRow.getCell(4).toString()); dsRecallResponseList.add(dsRecallResponse); } catch (Exception e){ logger.error("rowNum: {}" ,rowNum,e); } } } } } } catch (Exception e){ logger.error("read excel error!", e); } finally { is.close(); } return record; } }