import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
object RunWhileLoopForNSeconds_Scala {
def main(args: Array[String]): Unit = {
var startTime = LocalDateTime.now()
val seconds = 3
val endTime = startTime.plusSeconds(seconds)
while (startTime.isBefore(endTime)) {
Thread.sleep(1000)
val currentTime = LocalDateTime.now()
val formattedTime = currentTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
println(formattedTime)
startTime = currentTime
}
}
}
/*
run:
2024-10-12 14:20:48
2024-10-12 14:20:49
2024-10-12 14:20:50
*/