import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.ArrayList;
public class TimeExporter {
/**
* Exports time differences as a list of strings.
* @param startTimes A list of Instant objects representing start times.
* @param endTimes A list of Instant objects representing end times.
* @return A list of strings, where each string represents the duration
* between the corresponding start and end time. Returns an empty list
* if input lists are null or have different sizes.
*/
public static List<String> exportTimeDifferences(List<Instant> startTimes, List<Instant> endTimes) {
if (startTimes == null || endTimes == null || startTimes.size() != endTimes.size()) {
return new ArrayList<>(); // Return empty list for invalid input
}
List<String> results = new ArrayList<>();
for (int i = 0; i < startTimes.size(); i++) {
Duration duration = Duration.between(startTimes.get(i), endTimes.get(i));
results.add(duration.toString()); // Convert Duration to string
}
return results;
}
public static void main(String[] args) {
//Example Usage
List<Instant> startTimes = new ArrayList<>();
startTimes.add(Instant.now());
startTimes.add(Instant.now());
startTimes.add(Instant.now());
List<Instant> endTimes = new ArrayList<>();
endTimes.add(Instant.now().plusSeconds(5));
endTimes.add(Instant.now().plusSeconds(10));
endTimes.add(Instant.now().plusSeconds(15));
List<String> timeDifferences = exportTimeDifferences(startTimes, endTimes);
for (String diff : timeDifferences) {
System.out.println(diff);
}
}
}
Add your comment