比较作为文件名一部分的两个文件之间的时间



我有两个文件的时间戳不同

command_step_output/2019/02/13/ea768d46-85bb-4833-8326-fb6be6d60a89_20190213_105228_command_step_output.csv.gz
command_step_output/2019/02/13/ea768d46-85bb-4833-8326-fb6be6d60a89_20190213_105325_command_step_output.csv.gz

它们之间唯一的区别是文件名结束前的时间不同105228(意味着10:52:28)和105325(意味着10:00 53:25),我希望能够比较它们,并给它一个少1分钟或多1分钟的缓冲区,我尝试了一些方法,但没有给我解决办法。

java.time

计算两条路径中的时间差:

String onePath     = "command_step_output/2019/02/13/ea768d46-85bb-4833-8326-fb6be6d60a89_20190213_105228_command_step_output.csv.gz";
String anotherPath = "command_step_output/2019/02/13/ea768d46-85bb-4833-8326-fb6be6d60a89_20190213_105325_command_step_output.csv.gz";
LocalDateTime oneTime = extractDateTime(onePath);
LocalDateTime anboherTime = extractDateTime(anotherPath);
Duration diff = Duration.between(oneTime, anboherTime);
diff = diff.abs();

最后一行中对abs的调用将任何负差转换为正差,确保缓冲区少1分钟,多1分钟。extractDateTime在这个答案的底部。要知道差异是否小于一分钟,有不同的方法,我想向您展示一些选择。简单的第一个:

if (diff.toMinutes() < 1) {
System.out.println("Within the window: " + diff);
}

窗口内:PT57S

我已经打印了消息中的差异,它看起来有点有趣。格式为ISO 8601。读作"一段57秒的时间"。

上面的缺点是它只能工作几分钟。如果有一天你想把缓冲时间改为45秒或1分30秒怎么办?以下是更通用的:

Duration buffer = Duration.ofMinutes(1);
if (diff.compareTo(buffer) < 0) {
System.out.println("Within the window: " + diff);
}

我本来希望Duration有一个isShorterThan方法,但它没有。如果你发现使用compareTo的代码很难阅读,你并不孤单。另一种选择是相减,看看结果是否为负:

if (diff.minus(buffer).isNegative()) {
System.out.println("Within the window: " + diff);
}

我答应过你辅助方法的代码:

private static LocalDateTime extractDateTime(String path) {
String dateTimeString = path.replaceFirst("^.*/[0-9a-f-]+_(\d+_\d+)_command_step_output\.csv\.gz$", "$1");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuuMMdd_HHmmss"); 
return LocalDateTime.parse(dateTimeString, formatter);
}

我使用replaceFirst和正则表达式来提取20190213_105228部分。然后将其解析为LocalDateTime对象。

链接

  • Oracle教程:日期时间,解释如何使用java.Time
  • 维基百科文章:ISO 8601

首先提取日期:

private Date extractDate(String filename) {
// Updated to extract the date not from filename, but file and path name
int startDate = filename.indexof('_', filename.lastIndexof('/'));
int endDate = startDate + 15;
String dateStr = filename.substring(start, end);
// Use a date format for the part of string representing the dates
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss");
return format.parse(dateStr);
}

然后编写一个函数来检查提取的日期是否在小于1分钟的距离内。

public boolean samePeriod(String filename1, String filename2) {
Date date1 = extractDate(filename1);
Date date2 = extractDate(filename2);
long msDistance = Math.abs(date1.getTime() - date2.getTime());
// 1 minute is 1000 * 60 milliseconds
return msDistance <= 1000 * 60;
}

请注意,您必须通过检查null值和处理异常来丰富这个答案。这只是开发代码的基础。

如果您希望每个文件名都有通用比较器,请检查此

public static void compareStringsByChar(String in1,String in2) {
//assuming same strings
if(in1.length() == in2.length()) {
//collector of diffs
StringBuilder sbDiff = new StringBuilder();
//just for log bellow
int firstDiffIndex = -1;
//if diff is not in sequence, then will be used for putting comma in collected data
int lastDiffIndex = -1;
for (int i = 0; i < in1.length(); i++) {
//diff found
if(in2.charAt(i) != in1.charAt(i)) {
//first diff found
if(sbDiff.length() ==0 ) {
firstDiffIndex = i;
}
//checking if in sequence
if(lastDiffIndex != -1 && lastDiffIndex != (i-1)) {
sbDiff.append(",");
}
//finally add char diff and change reference to the last occurence
sbDiff.append(in2.charAt(i));
lastDiffIndex = i;
}
}
if(!sbDiff.toString().isEmpty()) {
System.out.println("Found difference at pos." + firstDiffIndex);
System.out.println("String1: " + in1.substring(firstDiffIndex));
System.out.println("String2: " + in2.substring(firstDiffIndex));
System.out.println("Diffs strings: " + sbDiff.toString());
}
}
}

对于您的数据:

String st1 = "command_step_output/2019/02/13/ea768d46-85bb-4833-8326-fb6be6d60a89_20190213_105228_command_step_output.csv.gz";
String st2 = "command_step_output/2019/02/13/ea768d46-85bb-4833-8326-fb6be6d60a89_20190213_105325_command_step_output.csv.gz";

输出:

Found difference at pos.80
String1: 228_command_step_output.csv.gz
String2: 325_command_step_output.csv.gz
Diffs strings: 3,5
#in case of diff sequence:
Found difference at pos.80
String1: 228_command_step_output.csv.gz
String2: 345_command_step_output.csv.gz
Diffs strings: 345

最新更新