I input a single date and obtain an ordered collection of (12-hour) timestamps where the first element is guaranteed to start on that date. The timestamps can potentially span multiple days given one input. I don't believe there are any cases where timestamps are more than 24 hours apart.
I need a way to handle the times and convert them to full dates in the case of rollover in a way such that the date increments properly.
Both the Date and Time are stored as Date objects. (my input is actually a sorted ArrayList<Date>
as I have several hundred files to process)
For example:
> 2014.05.13
01:02:03 AM
10:54:21 PM
10:59:32 PM
11:34:00 PM
11:59:54 PM
12:01:00 AM
01:02:03 AM
I want a collection of Dates kind of like this:
[2014-05-13 01:02:03 AM,
2014-05-13 10:54:21 PM,
2014-05-13 10:59:32 PM,
2014-05-13 11:34:00 PM,
2014-05-13 11:59:54 PM,
2014-05-14 12:01:00 AM,
2014-05-14 01:02:03 AM]
What is a good way to create this collection? I am using Java 8.
Edit: thanks to the suggestion from https://stackoverflow.com/a/23646930/1467811 to add dates in a loop. Here's some pseudocode of my solution, since the parsing logic is a lot more complicated than this.
// 2014-01-21.165900-0500EST
// 2014-05-03.124529-0400EDT
DateFormat df = new SimpleDateFormat("yyyy-MM-dd.HHmmssZz", Locale.ENGLISH);
ArrayList<Date> logDates = new ArrayList<>();
// <snip> ... get array of dates ...
Collections.sort(logDates);
DateFormat msgDf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a", Locale.ENGLISH);
Date lastDate;
for (Date d : logDates) {
lastDate = d;
Path p = GetFilePathFromDate(d);
for (String line : Files.readAllLines(p)) {
String time = ExtractTimeFromLine(line); // ex. "12:01:00 AM"
Date msgDate = msgDf.parse(new SimpleDateFormat("yyyy-MM-dd")
.format(lastDate) + " " + time);
if (msgDate.before(lastDate)) { // must have passed over a day
Calendar curCal = Calendar.getInstance();
curCal.setTime(msgDate);
curCal.add(Calendar.DAY_OF_MONTH, 1);
msgDate = curCal.getTime();
}
lastDate = msgDate;
// msgDate now has the correct date associated with its time
// ex. "2014-05-14 12:01:00 AM"
// do stuff with msgDate
}
}