Java 将一段时间划分成若干段小时间

Java 将一段时间划分成若干段小时间

工具类代码

package com.eve.wms.org.utils;

import com.eve.wms.common.exception.ServiceException;
import com.eve.wms.org.domain.vo.TimeSlotVo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 字符串转sql时间类型
 * @author leo
 * @date 2022-10-25
 */
@Component
public class StrToSqlTimeUtil {

    public static final String HH_MM_FORMAT = "HH:mm";

    public static final String TIME_PERIOD_CONNECTOR = "-";

    @PostConstruct
    public void init() {
        StrToSqlTimeUtil strToT = this;
    }

    public static Time strToTime(String strTime) {
        return new Time(strToDate(strTime).getTime());
    }

    public static Date strToDate(String strDate) {
        SimpleDateFormat format = new SimpleDateFormat(HH_MM_FORMAT);
        Date d;
        try {
            d = format.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new ServiceException("时间转换错误!");
        }
        if (d == null) {
            throw new ServiceException("时间转换为空!");
        }
        return d;
    }

    /**
     * 将时间按三十分钟进行切割
     * @param strTimeList 时间段数组字符串 如8:00-9:00,14:00-17:00
     * @return 切割后的List<Time>
     */
    public static List<TimeSlotVo> splitAccordingToThirtyMinutes(String strTimeList) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        //30分钟
        long timeLong = 30*60*1000;
        List<TimeSlotVo> result = new ArrayList<>();
        // 第一层:08:30-11:30,13:00-21:00
        String[] arr = strTimeList.split(",");
        for (String str:arr) {
            //08:30-11:30
            String[] strTimes = str.split("-");
            Date starDate = strToDate(strTimes[0]);
            Date endDate = strToDate(strTimes[1]);
            // 开始时间按30分钟循环到结束时间
            int index = 0;
            while (starDate.before(endDate)) {
                String firstTime = format.format(starDate);
                starDate.setTime(starDate.getTime()+timeLong);
                String secondTime = format.format(starDate);
                TimeSlotVo vo = new TimeSlotVo();
                vo.setSortNo(index);
                vo.setDisabled(false);
                vo.setTimeSlot(firstTime + TIME_PERIOD_CONNECTOR + secondTime);
                // 将 30分钟一段的时间存进集合
                result.add(vo);
            }
        }
        return result;
    }
}

存放时间的实体类

package com.eve.wms.org.domain.vo;

/**
 * 存放时间段数据Vo
 *
 * @author leo
 * @date 2022-09-02
 */
public class TimeSlotVo {

    private static final long serialVersionUID = 1L;

    private Integer sortNo;

    private String timeSlot;

    private Boolean disabled;

    public Integer getSortNo() {
        return sortNo;
    }

    public void setSortNo(Integer sortNo) {
        this.sortNo = sortNo;
    }

    public String getTimeSlot() {
        return timeSlot;
    }

    public void setTimeSlot(String timeSlot) {
        this.timeSlot = timeSlot;
    }

    public Boolean getDisabled() {
        return disabled;
    }

    public void setDisabled(Boolean disabled) {
        this.disabled = disabled;
    }
}

输入:“08:30-11:30,13:00-21:00”

得到:

[
    {
        "sortNo": 0,
        "timeSlot": "08:30-09:00",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "09:00-09:30",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "09:30-10:00",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "10:00-10:30",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "10:30-11:00",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "11:00-11:30",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "13:00-13:30",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "13:30-14:00",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "14:00-14:30",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "14:30-15:00",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "15:00-15:30",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "15:30-16:00",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "16:00-16:30",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "16:30-17:00",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "17:00-17:30",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "17:30-18:00",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "18:00-18:30",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "18:30-19:00",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "19:00-19:30",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "19:30-20:00",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "20:00-20:30",
        "disabled": false
    },
    {
        "sortNo": 0,
        "timeSlot": "20:30-21:00",
        "disabled": false
    }
]
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值