Reputation: 451
Like the title says, given two dates with times like
time1= "2017-01-31 12:00:00"
time2= "2017-01-31 15:00:00”
I want to return all timestamps within an array that are between these two times and dates. For example this is what the array will look like:
2017-01-31 01:33:30 random text log message x
2017-01-31 08:34:30 sdfsd log message y
2017-01-31 11:35:30 sdfsdfsdf log message z
2017-01-31 12:30:30 random text log message x
2017-01-31 13:31:30 sdfsd log message y
2017-01-31 14:32:30 sdfsdfsdf log message z
2017-01-31 16:32:35 sdfsdfsdf log message a
2017-01-31 16:33:30 random text log message x
2017-01-31 16:34:30 sdfsd log message y
2017-01-31 16:35:30 sdfsdfsdf log message z
2017-01-31 16:36:35 sdfsdfsdf log message a
Output:
2017-01-31 12:30:30 random text log message x
2017-01-31 13:31:30 sdfsd log message y
2017-01-31 14:32:30 sdfsdfsdf log message z
Would I have to use regex to store the time and date of each separate time/date stamp and then compare them to each timestamp in the array? Or what would be an optimal solution?
Upvotes: 0
Views: 435
Reputation: 86272
DateTimeFormatter logDtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
String[] logMessages = {
"2017-01-31 01:33:30 random text log message x",
"2017-01-31 08:34:30 sdfsd log message y",
"2017-01-31 11:35:30 sdfsdfsdf log message z",
"2017-01-31 12:30:30 random text log message x",
"2017-01-31 13:31:30 sdfsd log message y",
"2017-01-31 14:32:30 sdfsdfsdf log message z",
"2017-01-31 16:32:35 sdfsdfsdf log message a",
"2017-01-31 16:33:30 random text log message x",
"2017-01-31 16:34:30 sdfsd log message y",
"2017-01-31 16:35:30 sdfsdfsdf log message z",
"2017-01-31 16:36:35 sdfsdfsdf log message a"
};
String time1 = "2017-01-31 12:00:00";
String time2 = "2017-01-31 15:00:00";
LocalDateTime start = LocalDateTime.parse(time1, logDtf);
LocalDateTime end = LocalDateTime.parse(time2, logDtf);
Arrays.stream(logMessages)
.filter(lm -> {
TemporalAccessor parsedDateTime = logDtf.parse(lm, new ParsePosition(0));
LocalDateTime logDateTime = LocalDateTime.from(parsedDateTime);
return ! logDateTime.isBefore(start)
&& logDateTime.isBefore(end);
})
.forEach(System.out::println);
The output from this code is what you asked for:
2017-01-31 12:30:30 random text log message x 2017-01-31 13:31:30 sdfsd log message y 2017-01-31 14:32:30 sdfsdfsdf log message z
I am using java.time, the modern Java date and time API. I always recommend it for any date-time work in Java. I find it so nice to work with.
I am filtering to the half-open interval between your times. This means, a log message stamped exactly 2017-01-31 12:00:00 will be included, while a message stamped 2017-01-31 15:00:00 will not. This is usual and recommended use of time intervals. One advantage is if you filter to adjacent (non-overlaping) intervals, each log message is guaranteed to be included exactly once.
If your array is very large and always sorted and performance matters, you can benefit from using binary search for the start and end of the desired interval. Look it up if it’s relevant and you don’t already know what it is.
Link: Oracle tutorial: Date Time explaining how to use java.time
.
Upvotes: 3
Reputation: 1356
well, you know the date is 20 characters long, so you can get the date string using substring and then parse it with DateFormat to check if it is between the 2 dates. you parse time1, time2, and the your timestamp into dates, and then check if it is between the two times like this. timestamp.after(time1) && timestamp .before(time2)
String time1= "2017-01-31 12:00:00"
String time2= "2017-01-31 15:00:00”
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date minDate = format.parse(time1);
Date maxDate = format.parse(time2);
for(String row : somearray){
String rowDateString = row.substring(0,19);
Date rowDate = format.parse(rowDateString);
if(rowDate.after(minDate) && rowDate .before(maxDate){
//get this row because it is between dates
}
}
Upvotes: 1