Skip to content

Commit

Permalink
Merge pull request #100 from xkzhangsan/dev
Browse files Browse the repository at this point in the history
NLP解析时分秒时间组合问题修改
  • Loading branch information
xkzhangsan authored Dec 22, 2021
2 parents 4c89ee5 + 92a754a commit 72ddc32
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2571,6 +2571,128 @@ public static boolean isChineseWorkDay(LocalDateTime localDateTime, String holid
return isWorkDay(localDateTime);
}

/**
* 判断时间段是否包含工作日,节假日数据holidayData,如果节假日数据不支持年份,将使用周一到周五为工作日来判断。
* @param start 开始 时间戳
* @param end 结束 时间戳
* @param holidayData 放假信息0表示放假,1表示工作日,如:2021-01-01:0,2021-02-07:1
* @return boolean true 包含工作日 false 不包含
*/
public static boolean hasChineseWorkDay(long start, long end, String holidayData){
Objects.requireNonNull(holidayData, "holidayData");
LocalDateTime startDate = DateTimeConverterUtil.toLocalDateTime(start);
LocalDateTime endDate = DateTimeConverterUtil.toLocalDateTime(end);
List<LocalDateTime> dateList = getLocalDateTimeList(startDate, endDate, ChronoUnit.DAYS);
Map<String, Integer> dateTypeMap = StringUtil.convertHolidayDataToMapUseCache(holidayData);
for(LocalDateTime date : dateList){
String dateStr = DateTimeFormatterUtil.formatToDateStr(date);
Integer dateType = dateTypeMap.get(dateStr);
if(dateType != null){
if(dateType == 1){
return true;
}
}else{
if(isWorkDay(date)){
return true;
}
}
}
return false;
}

/**
* 判断时间段是否包含工作日,节假日数据holidayData,如果节假日数据不支持年份,将使用周一到周五为工作日来判断。
* @param start 开始 时间戳
* @param end 结束 时间戳
* @param holidayData 放假信息0表示放假,1表示工作日,如:2021-01-01:0,2021-02-07:1
* @return boolean true 包含工作日 false 不包含
*/
public static boolean hasChineseWorkDay(String start, String end, String holidayData){
Objects.requireNonNull(start, "start");
Objects.requireNonNull(end, "end");
Objects.requireNonNull(holidayData, "holidayData");
return hasChineseWorkDay(Long.parseLong(start), Long.parseLong(end), holidayData);
}

/**
* 判断时间段是否包含工作日,节假日数据holidayData,如果节假日数据不支持年份,将使用周一到周五为工作日来判断。
* @param start 开始 Date
* @param end 结束 Date
* @param holidayData 放假信息0表示放假,1表示工作日,如:2021-01-01:0,2021-02-07:1
* @return boolean true 包含工作日 false 不包含
*/
public static boolean hasChineseWorkDay(Date start, Date end, String holidayData){
Objects.requireNonNull(start, "start");
Objects.requireNonNull(end, "end");
Objects.requireNonNull(holidayData, "holidayData");
return hasChineseWorkDay(DateTimeConverterUtil.toEpochMilli(start), DateTimeConverterUtil.toEpochMilli(end), holidayData);
}

/**
* 判断时间段是否包含工作日,节假日数据holidayData,如果节假日数据不支持年份,将使用周一到周五为工作日来判断。
* @param start 开始 LocalDate
* @param end 结束 LocalDate
* @param holidayData 放假信息0表示放假,1表示工作日,如:2021-01-01:0,2021-02-07:1
* @return boolean true 包含工作日 false 不包含
*/
public static boolean hasChineseWorkDay(LocalDate start, LocalDate end, String holidayData){
Objects.requireNonNull(start, "start");
Objects.requireNonNull(end, "end");
Objects.requireNonNull(holidayData, "holidayData");
return hasChineseWorkDay(DateTimeConverterUtil.toEpochMilli(start), DateTimeConverterUtil.toEpochMilli(end), holidayData);
}

/**
* 时间段内中国工作日,节假日数据holidayData,如果节假日数据不支持年份,将使用周一到周五为工作日来判断。
* @param start 开始 时间戳
* @param end 结束 时间戳
* @param holidayData 放假信息0表示放假,1表示工作日,如:2021-01-01:0,2021-02-07:1
* @return 返回 工作日, yyyy-MM-dd 英文逗号分隔
*/
public static List<String> chineseWorkDay(long start, long end, String holidayData){
Objects.requireNonNull(holidayData, "holidayData");
List<String> result = new ArrayList<>();
LocalDate startDate = DateTimeConverterUtil.toLocalDate(start);
LocalDate endDate = DateTimeConverterUtil.toLocalDate(end);
List<LocalDate> dateList = getLocalDateList(startDate, endDate, ChronoUnit.DAYS);
Map<String, Integer> dateTypeMap = StringUtil.convertHolidayDataToMapUseCache(holidayData);
dateList.stream().forEach(date->{
String dateStr = date.toString();
Integer dateType = dateTypeMap.get(dateStr);
if(dateType != null){
if(dateType == 1){
result.add(dateStr);
}
}else{
if(isWorkDay(date)){
result.add(dateStr);
}
}
});
return result;
}

/**
* 时间段内中国工作日,节假日数据holidayData,如果节假日数据不支持年份,将使用周一到周五为工作日来判断。
* @param start 开始 时间戳
* @param end 结束 时间戳
* @param holidayData 放假信息0表示放假,1表示工作日,如:2021-01-01:0,2021-02-07:1
* @return 返回 工作日, yyyy-MM-dd 英文逗号分隔
*/
public static List<String> chineseWorkDay(LocalDate start, LocalDate end, String holidayData){
return chineseWorkDay(DateTimeConverterUtil.toEpochMilli(start), DateTimeConverterUtil.toEpochMilli(end), holidayData);
}
/**
* 时间段内中国工作日天数,节假日数据holidayData,如果节假日数据不支持年份,将使用周一到周五为工作日来判断。
* @param start 开始 时间戳
* @param end 结束 时间戳
* @param holidayData 放假信息0表示放假,1表示工作日,如:2021-01-01:0,2021-02-07:1
* @return 返回 工作日, yyyy-MM-dd 英文逗号分隔
*/
public static int chineseWorkDayCount(long start, long end, String holidayData){
return chineseWorkDay(start, end, holidayData).size();
}

/**
* 判断是否中国工作日,包含法定节假日调整日期,节假日数据holidayData,如果节假日数据不支持年份,将使用周一到周五为工作日来判断。
* @param localDate LocalDate
Expand Down Expand Up @@ -3554,9 +3676,12 @@ public static List<LocalDateTime> getLocalDateTimeList(LocalDateTime start, Loca
if(start.isAfter(end)){
throw new DateTimeException("start must before or equal end!");
}

int i = 1;
List<LocalDateTime> localDateTimeList = new ArrayList<LocalDateTime>();
if(start.equals(end)){
localDateTimeList.add(start);
return localDateTimeList;
}
int i = 1;
LocalDateTime localDateTime = start;
localDateTimeList.add(localDateTime);
while(localDateTime.isBefore(end)){
Expand Down Expand Up @@ -3596,6 +3721,40 @@ public static List<LocalDate> getLocalDateList(LocalDate start, LocalDate end){
.map(localDateTime -> localDateTime.toLocalDate()).collect(Collectors.toList());
}

/**
* 获取指定区间的时间列表,包含起始,间隔指定单位的相同时间
* @param start 开始时间
* @param end 结束时间
* @param unit 单位
* @return 时间列表
*/
public static List<LocalDate> getLocalDateList(LocalDate start, LocalDate end, ChronoUnit unit){
Objects.requireNonNull(start, "start");
Objects.requireNonNull(end, "end");
Objects.requireNonNull(unit, "unit");
if(start.isAfter(end)){
throw new DateTimeException("start must before or equal end!");
}
List<LocalDate> localDateList = new ArrayList<LocalDate>();
if(start.equals(end)){
localDateList.add(start);
return localDateList;
}
int i = 1;
LocalDate localDate = start;
localDateList.add(localDate);
while(localDate.isBefore(end)){
localDate = (LocalDate) plus(start, unit, i);
if(localDate.isAfter(end) || localDate.equals(end)){
break;
}
localDateList.add(localDate);
i++;
}
localDateList.add(end);
return localDateList;
}

/**
* 获取指定区间的时间列表,包含起始
* @param start 开始时间
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,34 @@ public static LocalTime toLocalTime(ZonedDateTime zonedDateTime) {
Objects.requireNonNull(zonedDateTime, "zonedDateTime");
return zonedDateTime.toLocalTime();
}

/**
* 时间戳的毫秒转LocalTime
* @param epochMilli 时间戳的毫秒
* @return LocalTime
*/
public static LocalTime toLocalTime(long epochMilli) {
return toLocalDateTime(epochMilli).toLocalTime();
}

/**
* 时间部分的毫秒转LocalTime
* @param timeMilli 时间部分的毫秒
* @return LocalTime
*/
public static LocalTime toLocalTime(int timeMilli) {
return LocalTime.ofNanoOfDay((long)timeMilli * 1000_000);
}

/**
* localTime转时间部分的毫秒
* @param localTime localTime
* @return 时间部分的毫秒
*/
public static int toTimeMilli(LocalTime localTime) {
Objects.requireNonNull(localTime, "localTime");
return (int) (localTime.toNanoOfDay() / 1000_000);
}

/**
* Date转Instant
Expand Down Expand Up @@ -480,6 +508,17 @@ public static long toEpochMilli(LocalDate localDate){
return toInstant(localDate).toEpochMilli();
}

/**
* LocalTime转时间戳
* 从1970-01-01T00:00:00Z开始的毫秒值
* 转换时会加上当天日期部分组成localDateTime再转换
* @param localDateTime LocalDateTime
* @return 时间戳
*/
public static long toEpochMilli(LocalTime localTime){
return toInstant(localTime).toEpochMilli();
}

/**
* Instant转时间戳
* 从1970-01-01T00:00:00Z开始的毫秒值
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/com/xkzhangsan/time/nlp/TimeNLP.java
Original file line number Diff line number Diff line change
Expand Up @@ -638,24 +638,28 @@ private void normBaseTimeRelated() {
localDateTime = localDateTime.plusMinutes(30);
}

boolean matchMinuteBeforeFlag = false;
pattern = RegexEnum.NormBaseTimeRelatedMinuteBefore.getPattern();
Matcher matchMinuteBefore = pattern.matcher(timeExpression);
if (matchMinuteBefore.find()) {
flag = true;
matchMinuteBeforeFlag = true;
int minute = Integer.parseInt(matchMinuteBefore.group());
localDateTime = localDateTime.minusMinutes(minute);
}

boolean matchMinuteAfterFlag = false;
pattern = RegexEnum.NormBaseTimeRelatedMinuteAfter.getPattern();
Matcher matchMinuteAfter = pattern.matcher(timeExpression);
if (matchMinuteAfter.find()) {
flag = true;
matchMinuteAfterFlag = true;
int minute = Integer.parseInt(matchMinuteAfter.group());
localDateTime = localDateTime.plusMinutes(minute);
}

//1个小时10分钟前,组合处理
if (matchMinuteBefore.find()) {
if (matchMinuteBeforeFlag) {
pattern = RegexEnum.NormBaseTimeRelatedHour.getPattern();
match = pattern.matcher(timeExpression);
if (match.find()) {
Expand All @@ -665,7 +669,7 @@ private void normBaseTimeRelated() {
}
}

if (matchMinuteAfter.find()) {
if (matchMinuteAfterFlag) {
pattern = RegexEnum.NormBaseTimeRelatedHour.getPattern();
match = pattern.matcher(timeExpression);
if (match.find()) {
Expand All @@ -675,23 +679,27 @@ private void normBaseTimeRelated() {
}
}

boolean matchSecondBeforeFlag = false;
pattern = RegexEnum.NormBaseTimeRelatedSecondBefore.getPattern();
Matcher matchSecondBefore = pattern.matcher(timeExpression);
if (matchSecondBefore.find()) {
flag = true;
matchSecondBeforeFlag = true;
int second = Integer.parseInt(matchSecondBefore.group());
localDateTime = localDateTime.minusSeconds(second);
}

boolean matchSecondAfterFlag = false;
pattern = RegexEnum.NormBaseTimeRelatedSecondAfter.getPattern();
Matcher matchSecondAfter = pattern.matcher(timeExpression);
if (matchSecondAfter.find()) {
flag = true;
matchSecondAfterFlag = true;
int second = Integer.parseInt(matchSecondAfter.group());
localDateTime = localDateTime.plusSeconds(second);
}

if (matchSecondBefore.find()) {
if (matchSecondBeforeFlag) {
pattern = RegexEnum.NormBaseTimeRelatedMinute.getPattern();
match = pattern.matcher(timeExpression);
if (match.find()) {
Expand All @@ -701,7 +709,7 @@ private void normBaseTimeRelated() {
}
}

if (matchSecondAfter.find()) {
if (matchSecondAfterFlag) {
pattern = RegexEnum.NormBaseTimeRelatedMinute.getPattern();
match = pattern.matcher(timeExpression);
if (match.find()) {
Expand Down
Loading

0 comments on commit 72ddc32

Please sign in to comment.