title: Sequential dates and times in Java
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>Both the Date and Time are stored as Date objects. (my input is actually a sorted <code>ArrayList&lt;Date&gt;</code> as I have several hundred files to process)</p>
  
  <p>For example:</p>
  
  <pre><code>&gt; 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
  </code></pre>
  
  <p>I want a collection of Dates kind of like this:</p>
  
  <pre><code>[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]
  </code></pre>
  
  <p>What is a good way to create this collection? I am using Java 8.</p>
  
  <hr>
  
  <p>Edit: thanks to the suggestion from <a href="https://stackoverflow.com/a/23646930/1467811">https://stackoverflow.com/a/23646930/1467811</a> to add dates in a loop. Here's some pseudocode of my solution, since the parsing logic is a lot more complicated than this.</p>
  
  <pre><code>// 2014-01-21.165900-0500EST
  // 2014-05-03.124529-0400EDT
  DateFormat df = new SimpleDateFormat("yyyy-MM-dd.HHmmssZz", Locale.ENGLISH);
  ArrayList&lt;Date&gt; logDates = new ArrayList&lt;&gt;();
  
  // &lt;snip&gt; ... 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
      }
  }
  </code></pre>