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
}
}
CodePudding user response:
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