import java.time.LocalDateTime
import java.time.temporal.ChronoUnit
object DateTimeDifferenceApp {
/**
* Calculates the total number of seconds between two LocalDateTime instances.
*
* @param startDateTime The starting date and time.
* @param endDateTime The ending date and time.
* @return Long The total number of seconds.
*/
def getSecondsBetween(startDateTime: LocalDateTime, endDateTime: LocalDateTime): Long = {
// ChronoUnit.SECONDS.between efficiently computes the total elapsed seconds
ChronoUnit.SECONDS.between(startDateTime, endDateTime)
}
def main(args: Array[String]): Unit = {
// Define two LocalDateTime instances
val dateTime1: LocalDateTime = LocalDateTime.of(2025, 1, 12, 0, 0, 0)
val dateTime2: LocalDateTime = LocalDateTime.of(2025, 1, 13, 0, 0, 0)
// Call the function to calculate the seconds difference
val secondsDifference: Long = getSecondsBetween(dateTime1, dateTime2)
// Output the results to the console
println(s"Start Date: $dateTime1")
println(s"End Date: $dateTime2")
println(s"Difference in seconds: $secondsDifference")
}
}
/*
run:
Start Date: 2025-01-12T00:00
End Date: 2025-01-13T00:00
Difference in seconds: 86400
*/