Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Stopping execution after 10 seconds/outputs

  1. #1
    Join Date
    Aug 2020
    Thanked 0 Times in 0 Posts

    Question Stopping execution after 10 seconds/outputs

    Hello, I'm messing around with LocalDateTime and I was wondering what method does one has to implement in order to make the timer stop after n times where n is the int the user enters. Also, is it my correct understanding that within 'java.time.LocalDateTime' the 'time' bit is the library, and the LocalDateTime is the class within it? Or is 'time' the package and 'LocalDateTime' the library of classes? I'll paste my program below.

    Note: You can just ignore the comments, they're more for me to help me memorize what each part does.

    import java.time.format.DateTimeFormatter;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.time.LocalDateTime;
    public class DisplayingCurrentDateTime extends TimerTask{
    	public static void main(String[] args) {
    		//This prints out the local date and time ONCE
    		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");  
    		LocalDateTime now = LocalDateTime.now();  
    		//This below outputs the time to console every second (1000ms)
    		Timer timer = new Timer();
    		timer.schedule(new DisplayingCurrentDateTime(), 0, 1000);//	timer.schedule(TimerTask task, long delay, long period)
    	}//end main
    	public void run() {
    		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");  
    		LocalDateTime now = LocalDateTime.now();  
    }//end class
    Last edited by HyperRei; August 11th, 2021 at 06:07 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    On vacation
    Thanked 2,678 Times in 2,628 Posts

    Default Re: Stopping execution after 10 seconds/outputs

    Look at the API doc for the Timer class. It has a method to stop the timer.
    Use a counter in the run method to determine when to stop the timer and call the Timer class's method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    HyperRei (August 11th, 2021)

Similar Threads

  1. TCP Socket connection sometimes takes several seconds
    By Melssj5 in forum Java Networking
    Replies: 0
    Last Post: March 27th, 2019, 12:02 PM
  2. Replies: 1
    Last Post: July 29th, 2014, 04:34 PM
  3. Time String to int seconds
    By willemuk in forum Java Theory & Questions
    Replies: 9
    Last Post: March 7th, 2012, 12:14 PM
  4. Setting Boolean to 'True' after X amount of Seconds?
    By uhKenKaniff in forum Java Theory & Questions
    Replies: 1
    Last Post: December 12th, 2011, 07:29 PM
  5. I want to extract minutes and seconds from duration
    By msa969 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 17th, 2011, 02:14 PM

Tags for this Thread