1

I am taking a CSV file of delivery times (unordered) and I need to sort them by time to use the data.

I am storing the necessary info in a LinkedList in my roadDataToBeSorted data structure which consists of 3 strings: rdName, rdZipCode, and rdDeliveryTime. My research so far says to use the LocalTime class since my time format in rdDeliveryTime is HH:MM:SS.

I'm not sure how I can turn my string into a LocalTime object I can then use the .isBefore() function to order my data.

for (int i = 0; i < stopOrderByDeliveryTime.size() - 1; i++) {
   if (LocalTime(stopOrderByDeliveryTime.get(i).rdDeliveryTime).isBefore(LocalTime(stopOrderByDeliveryTime.get(i+1).rdDeliveryTime))) {
      // add element at index (it is earlier than later elements)
   } else {
      // add element to end since it is later than all prior elements
   }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
CoolHands
  • 25
  • 7
  • Check out `LocalTime.parse`. – Jon Skeet Sep 27 '21 at 16:52
  • It is unclear what you're asking. Have you looked at the JavaDoc? What have you tried. [`LocalTime.parse(CharSequence)`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/time/LocalTime.html#parse(java.lang.CharSequence)) seems like a prime candidate. As an aside, using `LinkedList` almost never makes sense, an `ArrayList` almost always performs better and has significantly less memory overhead. – Mark Rotteveel Sep 27 '21 at 16:52
  • Yes, I missed that parse function, that fixed my issue and my isBefore method works as intended. And noted, I will switch to Arraylists and do some research into that, Thanks! – CoolHands Sep 27 '21 at 16:58

1 Answers1

4

In order to parse time, use LocalTime from Java 8. If your format is HH:MM:SS you can use:

    String timeString = "11:10:33";
    LocalTime iaka = LocalTime.parse(timeString, 
    DateTimeFormatter.ISO_LOCAL_TIME);

ISO_LOCAL_TIME is provided by Java. If however your time is NOT an ISO standard, let's say you need to parse: "hours-minutes:seconds.milis" then you will need to create your own formatter and pass it to LocalTime.parse:

    String timeString2 = "11-10:33.223";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH-mm:ss.nnn");
    System.out.println(LocalTime.parse(timeString2, formatter));

For more info, on how to make your own custom DateTimeFormatter.ofPattern take a look at the official java docs: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

moldovean
  • 3,132
  • 33
  • 36
  • 1
    `LocalTime.parse(CharSequence)` uses `DateTimeFormatter.ISO_LOCAL_TIME` by default, so there is no need to pass it explicitly. – Mark Rotteveel Sep 27 '21 at 17:16
  • @MarkRotteveel, yes, since this format is usually used in the western hemisphere it was overloaded by java: public static LocalTime parse(CharSequence text) { return parse(text, DateTimeFormatter.ISO_LOCAL_TIME); } But if you are not lucky with the strings you are getting, you'd better know how to create your own formatter – moldovean Sep 27 '21 at 17:21