我有2个文件,它们的时间戳有所不同
COL1 COL2 COL3
A null B
它们之间的唯一区别是文件名结尾之前的时间与105228(表示10:52:28)和105325(表示10:53:25)不同,我希望比较它们并在此示例中使用此逻辑为它提供少1分钟或多1分钟的缓冲区,文件名是相同的,并且我希望能够使用此缓冲区比较它们,我尝试了一些方法,但是它没有给我解决方案。
答案 0 :(得分:1)
首先提取日期:
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;
}
请注意,您必须通过检查空值和处理异常来丰富此答案。这只是开发代码的基础。
答案 1 :(得分:1)
要计算两条路径之间的时间差:
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
我已经在消息中打印出了区别,看起来有点有趣。格式为ISO8601。读取为“ 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
对象。
答案 2 :(得分:0)
如果您希望每个文件名都具有通用比较器,请选中此项
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