Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to run while loop for N seconds in Scala

1 Answer

0 votes
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
  
*/

 



answered Oct 12, 2024 by avibootz
edited Oct 12, 2024 by avibootz

Related questions

1 answer 93 views
1 answer 105 views
1 answer 95 views
2 answers 138 views
2 answers 132 views
2 answers 101 views
101 views asked Oct 11, 2024 by avibootz
1 answer 78 views
78 views asked Oct 11, 2024 by avibootz
...