Java 计算时间差,Java定时任务与excel数据读取。

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;

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值