注意:这是项目里的例子,具体需要什么需要自己挑选,灵活应用
package brilliance.znjt.dutymgrservice.utils;
import static org.mockito.Matchers.booleanThat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.print.attribute.standard.RequestingUserName;
import oracle.net.aso.j;
/**
* 判断警员已经部署的时段和将要部署的时段是否有交叉
* @author 35-pxiaodong
*
*/
public class IsAcrossUtil {
public static boolean isAcrossUtil(List<Map<String,Object>> sds,String qssj,String jssj) throws ParseException{
//确定一个日期
Date nowDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String nowDateStr = sdf.format(nowDate);
// System.out.println(nowDateStr);
//处理时间 如果为次日则日期加一
String qssjNoHas = getLastDate(nowDateStr,qssj);
String jssjNoHas = getLastDate(nowDateStr,jssj);
// System.out.println(qssjNoHas+" "+jssjNoHas);
for(Map<String, Object> map:sds){
String qssjHas = map.get("QSSJ").toString();
String jssjHas = map.get("JSSJ").toString();
qssjHas = getLastDate(nowDateStr, qssjHas);
jssjHas = getLastDate(nowDateStr, jssjHas);
// System.out.println(qssjHas+" "+jssjHas);
SimpleDateFormat simpledf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date qssjHasDate = simpledf.parse(qssjHas);
Date jssjHasDate = simpledf.parse(jssjHas);
Date qssjNoHasDate = simpledf.parse(qssjNoHas);
Date jssjNoHasDate = simpledf.parse(jssjNoHas);
// System.out.println(qssjHasDate+" "+jssjHasDate+" "+qssjNoHasDate+" "+jssjNoHasDate);
//判断两个时间段交叉
if (!(qssjNoHasDate.getTime() >= jssjHasDate.getTime() || jssjNoHasDate.getTime() <= qssjHasDate.getTime())) {
// return false;
// System.out.println("有交叉");
return false;
}else {
// System.out.println("没有交叉");
// return true;
}
}
// System.out.println(qssjNoHas);
return true;
}
public static String getLastDate(String nowDateStr, String time) throws ParseException {
String result = "";
if (time.contains("当日")) {
nowDateStr=nowDateStr;
}else if (time.contains("次日")) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
Date dateD = sdf.parse(nowDateStr);
String str2 = sdf.format(dateD);
// System.out.println(str2);
String year = str2.substring(0, 4);
String month = str2.substring(5, 7);
String day = str2.substring(8, 10);
// System.out.println(year+" "+month+" "+day);
Calendar c = Calendar.getInstance();
c.set(Integer.parseInt(year),Integer.parseInt(month), Integer
.parseInt(day)); c.add(Calendar.DAY_OF_MONTH, 1);
year = Integer.toString(c.get(Calendar.YEAR));
month = Integer.toString(c.get(Calendar.MONTH));
day = Integer.toString(c.get(Calendar.DATE));
nowDateStr = year+"-"+month+"-"+day;
}
time = time.substring(2);
result = nowDateStr+" "+time+":00";
// System.out.println(result);
return result;
}
public static void main(String[] args) throws ParseException {
/*List<Map<String, Object>> listMap = new ArrayList<>();
Map<String, Object> map = new HashMap<>();
Map<String, Object> map1 = new HashMap<>();
Map<String, Object> map2 = new HashMap<>();
map.put("QSSJ","当日8:00");
map.put("JSSJ","当日10:00");//2018-11-06 8:00:00 2018-11-06 19:00:00
listMap.add(map);
map2.put("QSSJ","当日11:00");
map2.put("JSSJ","次日14:00");//2018-11-06 8:00:00 2018-11-7 19:00:00
listMap.add(map2);
System.out.println(isAcrossUtil(listMap,"当日10:00","当日12:00"));;//2018-11-06 5:00:00 2018-11-06 7:00:00
*/
System.out.println(abc());
}
public static boolean abc() throws ParseException{
List<String> bsxxGgwsdStr = new ArrayList<>();
bsxxGgwsdStr.add("当日8:00,当日10:00");
bsxxGgwsdStr.add("当日10:00,当日12:00");
bsxxGgwsdStr.add("当日8:00,次日10:00");
// bsxxGgwsdStr.add("当日8:00,当日10:00");
int count = 0;
boolean result = true;
for(int i=0;i<bsxxGgwsdStr.size();i++){
for(int j=i+1;j<bsxxGgwsdStr.size();j++){
// System.out.println(bsxxGgwsdStr.get(i)+"和"+bsxxGgwsdStr.get(j));
List<Map<String, Object>> listMap = new ArrayList<>();
Map<String, Object> map = new HashMap<>();
map.put("QSSJ",bsxxGgwsdStr.get(i).split(",")[0]);
map.put("JSSJ",bsxxGgwsdStr.get(i).split(",")[1]);
listMap.add(map);
System.out.println(listMap.toString());
String qssj = bsxxGgwsdStr.get(j).split(",")[0];
String jssj = bsxxGgwsdStr.get(j).split(",")[1];
System.out.println(qssj+" "+jssj);
if (!isAcrossUtil(listMap, qssj, jssj)) {
result=false;
return false;
}
}
if (!result) {
return false;
}
}
return result;
}
}
java判断两个时间段是否有交叉
猜你喜欢
转载自blog.csdn.net/qq_36421955/article/details/95314004
今日推荐
周排行