热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

计算多段时间的重叠

packagecom.lihong.DDPush.pms;importcom.lihong.DDPush.mybatis.Parser;importorg.junit.Test;impor
package com.lihong.DDPush.pms;

import com.lihong.DDPush.mybatis.Parser;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* Created by lihong10 on 2017/9/28.
*/

public class TimeOverlapTest {

public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

@Test
public void test() {
String[] params = new String[] {
"2017-09-28 08:00:00",
"2017-09-28 10:00:00",
"2017-09-28 12:00:00",
"2017-09-28 15:00:00",
"2017-09-28 07:00:00",
"2017-09-28 18:00:00"
};

String[] params1 = new String[] {
"2017-09-28 08:00:00",
"2017-09-28 10:00:00",
"2017-09-28 12:00:00",
"2017-09-28 15:00:00"
};

String[] params2 = new String[] {
"2017-09-28 07:00:00",
"2017-09-28 18:00:00"
};

caculate(params, params1, params2);
}


@Test
public void test01() {
String[] params = new String[]{
"2017-09-28 08:00:00",
"2017-09-28 17:00:00",
"2017-09-28 09:00:00",
"2017-09-28 16:00:00"
};
String[] params1 = new String[]{
"2017-09-28 08:00:00",
"2017-09-28 17:00:00"
};

String[] params2 = new String[]{
"2017-09-28 09:00:00",
"2017-09-28 16:00:00"
};
caculate(params, params1, params2);
}


@Test
public void test02() {
String[] params = new String[]{
"2017-09-28 08:00:00",
"2017-09-28 10:00:00",
"2017-09-28 09:00:00",
"2017-09-28 12:00:00",
"2017-09-28 11:00:00",
"2017-09-28 13:00:00",
"2017-09-28 12:00:00",
"2017-09-28 15:00:00"
};

String[] params1 = new String[]{
"2017-09-28 08:00:00",
"2017-09-28 10:00:00",
"2017-09-28 09:00:00",
"2017-09-28 12:00:00",
"2017-09-28 11:00:00",
"2017-09-28 13:00:00"
};

String[] params2 = new String[]{
"2017-09-28 12:00:00",
"2017-09-28 15:00:00"
};

caculate(params, params1, params2);
}


@Test
public void test03() {
String[] params = new String[]{
"2017-09-28 08:00:00",
"2017-09-28 10:00:00",
"2017-09-28 09:00:00",
"2017-09-28 12:00:00",
"2017-09-28 11:00:00",
"2017-09-28 13:00:00",
"2017-09-28 12:00:00",
"2017-09-28 15:00:00",
"2017-09-28 07:00:00",
"2017-09-28 11:00:00"
};

String[] params1 = new String[]{
"2017-09-28 08:00:00",
"2017-09-28 10:00:00",
"2017-09-28 09:00:00",
"2017-09-28 12:00:00",
"2017-09-28 11:00:00",
"2017-09-28 13:00:00"
};

String[] params2 = new String[]{
"2017-09-28 12:00:00",
"2017-09-28 15:00:00",
"2017-09-28 07:00:00",
"2017-09-28 11:00:00"
};

caculate(params, params1, params2);
}


@Test
public void test04() {
String[] params = new String[]{
"2017-09-28 08:00:00",
"2017-09-28 10:00:00",
"2017-09-28 09:00:00",
"2017-09-28 12:00:00",
"2017-09-28 11:00:00",
"2017-09-28 13:00:00",
"2017-09-28 12:00:00",
"2017-09-28 15:00:00",
"2017-09-28 07:00:00",
"2017-09-28 11:00:00"
};

String[] params1 = new String[] {
"2017-09-28 08:00:00",
"2017-09-28 10:00:00",
"2017-09-28 09:00:00",
"2017-09-28 12:00:00",
"2017-09-28 11:00:00",
"2017-09-28 13:00:00"
};

String[] params2 = new String[]{
"2017-09-28 12:00:00",
"2017-09-28 15:00:00",
"2017-09-28 07:00:00",
"2017-09-28 11:00:00"
};

caculate(params, params1, params2);
}


/**
* @Author: lihong10
* @Description:
* @param
* @Date: 2017/9/30 14:34
*/

@Test
public void test05() {
String[] params = new String[] {
"2017-09-28 07:00:00",
"2017-09-28 10:00:00",
"2017-09-28 13:00:00",
"2017-09-28 18:00:00",
"2017-09-28 21:00:00",
"2017-09-28 23:00:00",
"2017-09-28 01:00:00",
"2017-09-28 23:40:00"
};

String[] params1 = new String[] {
"2017-09-28 07:00:00",
"2017-09-28 10:00:00",
"2017-09-28 13:00:00",
"2017-09-28 18:00:00",
"2017-09-28 21:00:00",
"2017-09-28 23:40:00"
};

String[] params2 = new String[]{
"2017-09-28 01:00:00",
"2017-09-28 23:40:00"
};

caculateNotInHoliday(params, params1, params2);
}



private void caculateNotInHoliday(String[] params, String[] authTimes, String[] parkTimes) {
List dates = getDateList(params);
removeRepeat(dates);
Collections.sort(dates);


List pairs = new ArrayList();

for (int i = 0, j = dates.size(); i if ((i + 2) <= j) {

System.out.println("i: " + i + ", j: " + (i + 1));
TimePair pair = new TimePair(dates.get(i), dates.get(i + 1));
pairs.add(pair);
pair.caculateOverLapWithAuth(getDateList(authTimes));
pair.caculateOverLapWithStay(getDateList(parkTimes));
System.out.println(pair);
System.out.println();
}
}


System.out.println("===============================================================================");
System.out.println(pairs);
System.out.println("===============================================================================");
List notInHolidayLi = new ArrayList<>();
for (int i = 0; i TimePair pair = pairs.get(i);
if (pair.notInHoliday()) {
notInHolidayLi.add(pair);
System.out.println(pair);
}
}

System.out.println("===============================================================================");
System.out.println(notInHolidayLi);
System.out.println("===============================================================================");
System.out.println(mergeContinous(notInHolidayLi));

}


private void caculate(String[] params, String[] authTimes, String[] parkTimes) {
List dates = getDateList(params);
removeRepeat(dates);
Collections.sort(dates);


List pairs = new ArrayList();

for (int i = 0, j = dates.size(); i if ((i + 2) <= j) {

System.out.println("i: " + i + ", j: " + (i + 1));
TimePair pair = new TimePair(dates.get(i), dates.get(i + 1));
pairs.add(pair);
pair.caculateOverLapWithAuth(getDateList(authTimes));
pair.caculateOverLapWithStay(getDateList(parkTimes));
System.out.println(pair);
System.out.println();
}
}


System.out.println("===============================================================================");
System.out.println(pairs);
System.out.println("===============================================================================");
List chargeTimes = new ArrayList<>();
for (int i = 0; i TimePair pair = pairs.get(i);
if (pair.needCharge()) {
chargeTimes.add(pair);
System.out.println(pair);
}
}


System.out.println("===============================================================================");
System.out.println("-------------------------------------------------------------------------------");
System.out.println(chargeTimes);
System.out.println("-------------------------------------------------------------------------------");
System.out.println(mergeContinous(chargeTimes));

}

public void removeRepeat(List dates) {

for (int i = 0; i <(dates.size() - 1); i++) {
for (int j = i + 1; j System.out.print("remove i: " + i);
System.out.print("remove j: " + j);
if (dates.get(i).getTime() == dates.get(j).getTime()) {
dates.remove(j);
}
}
}
}


public List mergeContinous(List pairs) {

if (pairs == null || pairs.size() == 0) {
return new ArrayList();
}

List li = new ArrayList();

if (pairs.size() == 1) {
Pair p = new Pair(pairs.get(0).getStartTime(), pairs.get(0).getEndTime());
li.add(p);
return li;
}

Date start = pairs.get(0).getStartTime();
Date end = pairs.get(0).getEndTime();
for (int i = 1; i TimePair tp = pairs.get(i);
if (tp.getStartTime().getTime() == end.getTime()) {
end = tp.getEndTime();
} else {
Pair pr = new Pair(start, end);
li.add(pr);

start = tp.getStartTime();
end = tp.getEndTime();
}
}

li.add(new Pair(start, end));

return li;
}


public static boolean isCollectionEmpty(Collection collection) {
if (collection == null || collection.isEmpty()) {
return true;
}
return false;
}


public static Date stringToDate(String strValue, String strFormat) {
Date date = null;
try {
date = new SimpleDateFormat(strFormat).parse(strValue);
} catch (ParseException e) {
}
return date;
}

public static String dateToString(Date dateValue, String strFormat) {
return new SimpleDateFormat(strFormat).format(dateValue);
}


public static List getDateList(String... dateStr) {

if (dateStr == null || dateStr.length == 0) {
return new ArrayList<>();
}

List result = new ArrayList();

for (String el : dateStr) {
result.add(stringToDate(el, YYYY_MM_DD_HH_MM_SS));
}

return result;
}

private static class TimePair {

private Date startTime;
private Date endTime;

private Long startLong;
private Long endLong;

private String startString;
private String endString;

private int authOverlapCout;
private int stayOverlapCount;


private static final Predicate predicateIsCharge = new Predicate() {
@Override
public boolean test(TimePair timePair) {
return timePair.getStayOverlapCount() > timePair.getAuthOverlapCout();
}
};

private static final Predicate predicateNotInHoliday = new Predicate() {
@Override
public boolean test(TimePair timePair) {
int authOverlapCout = timePair.getAuthOverlapCout();
int stayOverlapCount = timePair.getStayOverlapCount();

//在该时段有停留,但是该时段不在节假日内
return stayOverlapCount > 0 && authOverlapCout == 0;
}
};

public TimePair(Long startLong, Long endLong) {
this(new Date(startLong), new Date(endLong));
}

public TimePair(Date startTime, Date endTime) {
this.startTime = startTime;
this.endTime = endTime;
this.startLOng= startTime.getTime();
this.endLOng= endTime.getTime();
this.startString = dateToString(startTime, YYYY_MM_DD_HH_MM_SS);
this.endString = dateToString(endTime, YYYY_MM_DD_HH_MM_SS);
}

public TimePair(String startString, String endString) {
this.startString = startString;
this.endString = endString;
this.startTime = stringToDate(startString, YYYY_MM_DD_HH_MM_SS);
this.endTime = stringToDate(endString, YYYY_MM_DD_HH_MM_SS);
this.startLOng= this.startTime.getTime();
this.endLOng= this.endTime.getTime();
}


public String getStartString() {
return startString;
}

public void setStartString(String startString) {
this.startString = startString;
}

public String getEndString() {
return endString;
}

public void setEndString(String endString) {
this.endString = endString;
}

public int getStayOverlapCount() {
return stayOverlapCount;
}

public void setStayOverlapCount(int stayOverlapCount) {
this.stayOverlapCount = stayOverlapCount;
}

public Date getStartTime() {
return startTime;
}

public void setStartTime(Date startTime) {
this.startTime = startTime;
}

public Date getEndTime() {
return endTime;
}

public void setEndTime(Date endTime) {
this.endTime = endTime;
}

public Long getStartLong() {
return startLong;
}

public void setStartLong(Long startLong) {
this.startLOng= startLong;
}

public Long getEndLong() {
return endLong;
}

public void setEndLong(Long endLong) {
this.endLOng= endLong;
}

public int getAuthOverlapCout() {
return authOverlapCout;
}

public void setAuthOverlapCout(int authOverlapCout) {
this.authOverlapCout = authOverlapCout;
}


public int caculateOverLapWith(Date startTime, Date endTime) {
if (this.startLOng== null) {
this.startLOng= this.startTime.getTime();
}

if (this.endLOng== null) {
this.endLOng= this.endTime.getTime();
}

int incr = 0;
if (this.startLong >= startTime.getTime() && this.endLong <= endTime.getTime()) {

if (this.startLong != this.endLong) {


incr += 1;
}
}


return incr;
}

public int caculateOverLapWithAuth(Date startTime, Date endTime) {
int incr = caculateOverLapWith(startTime, endTime);
if (incr > 0) {
String startTimeStr = dateToString(startTime, YYYY_MM_DD_HH_MM_SS);
String endTimeStr = dateToString(endTime, YYYY_MM_DD_HH_MM_SS);
System.out.println(Parser.parse1("[包期时间]: ({} ~ {})与时间段({} ~ {})有重叠", this.startString, this.endString, startTimeStr, endTimeStr));
}
this.authOverlapCout += incr;

return this.authOverlapCout;
}

public int caculateOverLapWithStay(Date startTime, Date endTime) {
int incr = caculateOverLapWith(startTime, endTime);
if (incr > 0) {
String startTimeStr = dateToString(startTime, YYYY_MM_DD_HH_MM_SS);
String endTimeStr = dateToString(endTime, YYYY_MM_DD_HH_MM_SS);
System.out.println(Parser.parse1("[停留时间]: ({} ~ {})与时间段({} ~ {})有重叠", this.startString, this.endString, startTimeStr, endTimeStr));
}
this.stayOverlapCount += incr;
return this.stayOverlapCount;
}

public boolean needCharge() {
return predicateIsCharge.test(this);
// return stayOverlapCount > authOverlapCout;
}

public boolean notInHoliday() {
return predicateNotInHoliday.test(this);
}


public int caculateOverLapWithAuth(List dates) {
int count = this.authOverlapCout;
if (dates == null) {
return count;
}

final int batch = 2;

while (dates.size() >= batch) {
List li = dates.subList(0, batch);
count = caculateOverLapWithAuth(li.get(0), li.get(1));
li.clear();
}

return count;
}

public int caculateOverLapWithStay(List dates) {
int count = this.stayOverlapCount;
if (dates == null) {
return count;
}

final int batch = 2;

while (dates.size() >= batch) {
List li = dates.subList(0, batch);
count = caculateOverLapWithStay(li.get(0), li.get(1));
li.clear();
}

return count;
}


@Override
public String toString() {
if (startString == null) {
startString = dateToString(startTime, YYYY_MM_DD_HH_MM_SS);
}

if (endString == null) {
endString = dateToString(endTime, YYYY_MM_DD_HH_MM_SS);
}

return "(" + startString + " ~ " + endString + ") : auth:" + this.authOverlapCout + " stay:" + this.stayOverlapCount;
}
}


private static class Pair {
public A first;
public B second;

public Pair(A first, B second) {
this.first = first;
this.secOnd= second;
}

@Override
public String toString() {
return Parser.parse1("<{}, {}>\n\t",
first instanceof Date ? dateToString((Date) first, YYYY_MM_DD_HH_MM_SS) : first,
second instanceof Date ? dateToString((Date) second, YYYY_MM_DD_HH_MM_SS) : second);
}
}


public interface Predicate {
public boolean test(T t);
}






}



推荐阅读
  • 深入解析Java枚举及其高级特性
    本文详细介绍了Java枚举的概念、语法、使用规则和应用场景,并探讨了其在实际编程中的高级应用。所有相关内容已收录于GitHub仓库[JavaLearningmanual](https://github.com/Ziphtracks/JavaLearningmanual),欢迎Star并持续关注。 ... [详细]
  • JavaScript 基础语法指南
    本文详细介绍了 JavaScript 的基础语法,包括变量、数据类型、运算符、语句和函数等内容,旨在为初学者提供全面的入门指导。 ... [详细]
  • 对象自省自省在计算机编程领域里,是指在运行时判断一个对象的类型和能力。dir能够返回一个列表,列举了一个对象所拥有的属性和方法。my_list[ ... [详细]
  • 本问题探讨了在特定条件下排列儿童队伍的方法数量。题目要求计算满足条件的队伍排列总数,并使用递推算法和大数处理技术来解决这一问题。 ... [详细]
  • 异常要理解Java异常处理是如何工作的,需要掌握一下三种异常类型:检查性异常:最具代表性的检查性异常是用户错误或问题引起的异常ÿ ... [详细]
  • 本文介绍如何使用 Android 的 Canvas 和 View 组件创建一个简单的绘图板应用程序,支持触摸绘画和保存图片功能。 ... [详细]
  • 本题探讨了在大数据结构背景下,如何通过整体二分和CDQ分治等高级算法优化处理复杂的时间序列问题。题目设定包括节点数量、查询次数和权重限制,并详细分析了解决方案中的关键步骤。 ... [详细]
  • 2018-2019学年第六周《Java数据结构与算法》学习总结
    本文总结了2018-2019学年第六周在《Java数据结构与算法》课程中的学习内容,重点介绍了非线性数据结构——树的相关知识及其应用。 ... [详细]
  • 实用正则表达式有哪些
    小编给大家分享一下实用正则表达式有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下 ... [详细]
  • 主板IO用W83627THG,用VC如何取得CPU温度,系统温度,CPU风扇转速,VBat的电压. ... [详细]
  • 本文探讨了使用C#在SQL Server和Access数据库中批量插入多条数据的性能差异。通过具体代码示例,详细分析了两种数据库的执行效率,并提供了优化建议。 ... [详细]
  • 方法:1 配置数据库basediros.path.abspath(os.path.dirname(__file__))  #获取当前文件的绝对路径appFlask(__name__ ... [详细]
  • 利用决策树预测NBA比赛胜负的Python数据挖掘实践
    本文通过使用2013-14赛季NBA赛程与结果数据集以及2013年NBA排名数据,结合《Python数据挖掘入门与实践》一书中的方法,展示如何应用决策树算法进行比赛胜负预测。我们将详细讲解数据预处理、特征工程及模型评估等关键步骤。 ... [详细]
  • 本文介绍了SVD(奇异值分解)和QR分解的基本原理及其在Python中的实现方法。通过具体代码示例,展示了如何使用这两种矩阵分解技术处理图像数据和计算特征值。 ... [详细]
  • 深入理解Vue.js:从入门到精通
    本文详细介绍了Vue.js的基础知识、安装方法、核心概念及实战案例,帮助开发者全面掌握这一流行的前端框架。 ... [详细]
author-avatar
稻米屋321
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有