时间分割与获取一下阶段时间

1.获取某段时间内的时间间隔,小时为单位,分钟为单位

    /**
     * 获取时间间隔,小时为单位
     * 
     * @param startTime
     * @param endTime
     * @return
     */
    private long getHourTimeSpace(String startTime, String endTime) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Date beginDate = null;
        ;
        Date endDate = null;
        try {
            beginDate = format.parse(startTime);
            endDate = format.parse(endTime);
        } catch (ParseException e) {
            logger.error("时间解析失败", e);
        }
        long space = (endDate.getTime() - beginDate.getTime()) / (60 * 60 * 1000);
        return space;
    }

    /**
     * 获取时间间隔,分钟为单位
     * 
     * @param startTime
     * @param endTime
     * @return
     */
    private long getMinuteTimeSpace(String startTime, String endTime) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Date beginDate = null;
        ;
        Date endDate = null;
        try {
            beginDate = format.parse(startTime);
            endDate = format.parse(endTime);
        } catch (ParseException e) {
            logger.error("时间解析失败", e);
        }
        long space = (endDate.getTime() - beginDate.getTime()) / (60 * 1000);
        return space;
    }

2.获取下一阶段的时间(下一小时,下一分钟)

    /**
     * 根据时间增量获取下一阶段时间(小时)
     * 
     * @param paramTime
     * @param increment
     * @return
     */
    private String getNextHourTime(String paramTime, int increment) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = null;
        try {
            date = (Date) formatter.parse(paramTime);
        } catch (ParseException e) {
            logger.error("时间格式化出错", e);
            return paramTime;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY, increment);
        Timestamp ts = new Timestamp(cal.getTimeInMillis());
        date = ts;
        return formatter.format(date);
    }

    /**
     * 获取下一分钟时间
     * @param paramTime
     * @param increment
     * @return
     */
    private String getNextMinuteTime(String paramTime, int increment) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = null;
        try {
            date = (Date) formatter.parse(paramTime);
        } catch (ParseException e) {
            logger.error("时间格式化出错", e);
            return paramTime;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, increment);
        Timestamp ts = new Timestamp(cal.getTimeInMillis());
        date = ts;
        return formatter.format(date);
    }

 

posted @ 2017-11-01 11:24  疯狂的蜗牛仔  阅读(730)  评论(0编辑  收藏  举报