import java.time.LocalDateTime;
import java.time.Duration;
public class DateDifferenceApp {
/**
* Calculates the total number of seconds between two LocalDateTime instances.
*
* @param startDate The starting date and time.
* @param endDate The ending date and time.
* @return long The total number of seconds.
*/
public static long getSecondsBetween(LocalDateTime startDate, LocalDateTime endDate) {
// Duration.between calculates the time interval between two temporal objects
Duration duration = Duration.between(startDate, endDate);
// Return the total elapsed seconds as a long
return duration.getSeconds();
}
public static void main(String[] args) {
// Define two LocalDateTime instances using clear factory methods (best practice)
LocalDateTime startDate = LocalDateTime.of(2026, 10, 27, 12, 0, 0);
LocalDateTime endDate = LocalDateTime.of(2026, 10, 27, 12, 5, 30);
// Call the function to calculate the seconds between the two dates
long totalSeconds = getSecondsBetween(startDate, endDate);
// Output the results to the console
System.out.println("Start Date: " + startDate);
System.out.println("End Date: " + endDate);
System.out.println("Total Seconds Between: " + totalSeconds);
}
}
/*
run:
Start Date: 2026-10-27T12:00
End Date: 2026-10-27T12:05:30
Total Seconds Between: 330
*/