java 时间的相关转换操作

本文介绍了一个实用的时间操作工具类,主要用于预测场景中时间片段的确定与划分。该工具类能够将一天的时间精确分割为多个等长段,支持从整点开始的业务需求,并提供方法来确定任意时间点所属的时间段。

关于时间的操作,写了个util
主要用于预测的时候,时间段的确定
预测的时间段,需要明确的规定范围,由于业务的促销都是整点开始,所以我们的预测时间段,也是从整点开始
所以就要将时间分为多个整段,比如一分钟一段,那么就是2018-06-18 00:00:00 到2018-06-18 00:01:00 就是一段
那么在确定一分钟为一段的前提下,我们就要将这一分钟内所有数据都归到这一分钟内
下面的util就有根据随机时间,确定这个时间是哪个段的
比如:
当前时间是2018-06-18 00:00:30 ,时间段为一分钟
根据下面的的方法,就可以得到2018-06-18 00:00:00和2018-06-18 00:01:00,也就是这个时间所在的时间区域

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间片分割工具类
 **/
public class TimeUtil {

   
    /**
     * 将一天的时间分割成等分的若干段,获得总段数
     *
     * @param second 每个时间段的长度,单位秒
     * @return 返回一天的段数, 结果取加一法的结果
     */
    public static int getAllSplitNums(int second) {
        return (24 * 60 * 60 - 1) / second + 1;
    }

    /**
     * 根据每段的时间和当前时间,算出当前时间在第几段
     * 时间段区间默认左闭右开
     * eg:如果5分钟为一个时间段的话,那么[00:00:00,00:05:00)就在第一段,返回1;[00:05:00,00:10:00)就在第二段,返回2
     *
     * @param time
     * @param second
     * @return
     */
    public static int getOneNum(long time, int second) {

        int secondNum = getSecondNum(time);

        return (secondNum - 1) / (second * 1000) + 1;
    }

    /**
     * 根据long类型的时间,获取时分秒的秒数
     *
     * @param time long类型的时间
     * @return 时分秒的秒数
     */
    public static int getSecondNum(long time) {
        Date date = new Date(time);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss:SSS");
        String format = simpleDateFormat.format(date);
        String[] split = format.split(":");

        int secondNum = 0;
        secondNum += Integer.valueOf(split[0]) * 60 * 60 * 1000;
        secondNum += Integer.valueOf(split[1]) * 60 * 1000;
        secondNum += Integer.valueOf(split[2]) * 1000;
        secondNum += Integer.valueOf(split[3]);
        return secondNum;
    }

    /**
     * 根据但当前时间和时间段长度,获得当前时间所在的时间段
     *
     * @param reqTime 入参时间,eg:1535551963527,毫秒级
     * @param second 间隔时间
     * @return 返回入参时间所在的时间段,左开右闭
     */
    public static String getSameTime(String reqTime, int second) {
        String s = null;
        try {
            Long reqTimeLong = Long.valueOf(reqTime);
            //获得这个时间在当天的第几段

            int secondNum = getSecondNum(reqTimeLong);
            long dayTime = reqTimeLong - secondNum;

            int oneNum = getOneNum(reqTimeLong, second);
            long end = dayTime + oneNum * second * 1000;
            long start = dayTime + (oneNum - 1) * second * 1000;

            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");
            s = sdf2.format(start) + ":" + sdf2.format(end);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }


    /**
     * 是不是整时段,比如时间段是30,那么只有秒数是00或者30的才会返回true,其他情况返回false
     *
     * @param timeInterval 时间段,单位秒
     * @return 布尔值
     */
    public static Boolean isClock(Integer timeInterval) {
        if(timeInterval == null || timeInterval.equals(0)){
            return null;
        }
        long now = System.currentTimeMillis();
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHH-mm-ss");
        String nowStr = sdf2.format(now);
        String[] split = nowStr.split("-");
        //只取分秒
        int res = Integer.valueOf(split[1]) * 60 + Integer.valueOf(split[2]);
        return res % timeInterval == 0;
    }

    /**
     * 根据本端时间获取下段时间
     * @param time 本段时间,格式必须是yyyyMMddHHmmss_yyyyMMddHHmmss的
     * @return 返回下段时间,格式也是yyyyMMddHHmmss_yyyyMMddHHmmss
     */
    public static String getNextTime(String time){
        String nextTime = null;
        try {
            String[] split = time.split(":");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            Date startDate = sdf.parse(split[0]);
            System.out.println("startDate:"+startDate);
            Date endDate = sdf.parse(split[1]);
            System.out.println("endDate:"+endDate);
            int timeInterval = (int) (endDate.getTime() - startDate.getTime()) / 1000;
            System.out.println("timeInterval:"+timeInterval);
            long nextTimeLong = endDate.getTime() + 1;
            nextTime = getSameTime(nextTimeLong + "", timeInterval);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nextTime;
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值