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;
}
}
传参:多个时间段用逗号分割 例如:“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
}
]