package com.bjtxjys.utils;
import org.junit.Test;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class TestLocalTime {
@Test
public void spiltTime() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Date defStart = sdf.parse("08:00");
Date defEnd = sdf.parse("20:59");
Date start1 = sdf.parse("08:00");
Date end1 = sdf.parse("10:30");
Date start2 = sdf.parse("15:00");
Date end2 = sdf.parse("17:00");
List
List
defDates.add(new Date[]{defStart, defEnd});
dates.add(new Date[]{start1, end1});
dates.add(new Date[]{start2, end2});
getFreeTimes(defDates, dates);
for(Date[] date : defDates){
System.out.println(sdf.format(date[0]) + "--" + sdf.format(date[1]));
}
}
@Test
public void spiltTime1(){
List
stringList.add("08:45-12:00");
stringList.add("14:00-18:00");
List
list.stream().forEach(e -> System.out.println(e));
}
private List
List
BigDecimal intervalValue = new BigDecimal(step);
timeSlots.stream().forEach(timeSlot -> {
String[] split = timeSlot.split("-");
LocalTime begin = LocalTime.parse(split[0]);
LocalTime end = LocalTime.parse(split[1]);
long between = begin.until(end, intervalUnit);
BigDecimal decimal = new BigDecimal(between);
BigDecimal divide = decimal.divide(intervalValue,2,BigDecimal.ROUND_HALF_UP);
List
.limit(divide.longValue()).collect(Collectors.toList());
result.addAll(list);
});
return result;
}
public static void getFreeTimes(List
for(Date[] date : dates){
dateFilter(defDates);
for(int i = 0; i < defDates.size(); i++){
Date[] defDate = defDates.get(i);
Date[][] d = getFreeTimes(defDate, date);
if(d != null){
defDates.remove(defDate);
i--;
if(d.length >= 1){
defDates.add(d[0]);
}
if(d.length == 2){
defDates.add(d[1]);
}
}
}
}
}
private static void dateFilter(List
if(dates != null){
for(int i = 0; i < dates.size(); i++){
Date[] date = dates.get(i);
if(date[0].getTime() == date[1].getTime()){
dates.remove(i);
i--;
}
}
}
}
private static Date[][] getFreeTimes(Date[] defDate, Date[] date){
Date[][] dates = new Date[2][2];
if(defDate != null && defDate.length == 2 && date != null && date.length == 2){
long defStartTime = defDate[0].getTime();
long defEndTime = defDate[1].getTime();
long startTime = date[0].getTime();
long endTime = date[1].getTime();
long start = startTime - defStartTime;
long end = defEndTime - endTime;
if(start < 0 || end < 0){
return null;
}
Date[] date1 = new Date[2];
date1[0] = new Date(defStartTime);
date1[1] = new Date(startTime);
Date[] date2 = new Date[2];
date2[0] = new Date(endTime);
date2[1] = new Date(defEndTime);
dates[0] = date1;
dates[1] = date2;
// System.out.println(sdf.format(dates[0][0]) + "--" + sdf.format(dates[0][1]));
// System.out.println(sdf.format(dates[1][0]) + "--" + sdf.format(dates[1][1]));
return dates;
}
return null;
}
}