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.


>> REGISTER NOW TO START POSTING


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

Page 1 of 4 123 ... LastLast
Results 1 to 25 of 81

Thread: Need some help in a timer

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Need some help in a timer

    Hello guys!
    I need some help to make a timer, a regressive timer.

    I need a countdown in days and hours until the next world cup (Brazil WC 2014), and am not sure aabout how it's done.

    I copied from somebody else the following code:
    package timer;
     
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.Timer;
     
    @SuppressWarnings("serial")
    public class Test extends JApplet {
     
        private JLabel label1 = new JLabel("60");
        private JLabel label2 = new JLabel("Dias até a copa");
        private int count = 60;
        private Timer timer;
     
        public Test() {
            JPanel panel1 = new JPanel(new GridLayout(1, 2));
            panel1.add(label1);
            panel1.add(label2);
            label1.setHorizontalAlignment(JLabel.CENTER);
            label2.setHorizontalAlignment(JLabel.CENTER);
            JPanel panel2 = new JPanel();
     
            add(panel1, BorderLayout.CENTER);
            add(panel2, BorderLayout.SOUTH);
     
            timer = new Timer(1000, new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                    count--;
                    if (count == 0) timer.stop();
     
                    label1.setText(String.valueOf(count));
     
                }
            });
            timer.start();
     
                }
            };


    And I need something like this:
    HFyYNDs.jpg
    That will get the PCs time and will get the time left until a predetermined date

    If anybody could put me in the right way to do it, I'd be thankfull!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,104
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: Need some help in a timer

    It's hard to help somebody with code they've copied from somewhere else, because we don't know what you don't know. Do you know Java or programming at all? We don't know.

    I suggest you start by reviewing and attempting to apply the Swing Timer Tutorial to the code you have that you can understand. When you get stuck, let us know, and we'll help if we can.

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

    Kerooker (April 25th, 2014)

  4. #3
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    I have some experience in Java and Programming (mainly lua... It's simple, but I can understand it)
    And I can understand mostly everything in Java, but I can't do it myself. Most of the times I get lots of codes from lots of people and sort of mix them up until I have some result.

    But in this case, I can't go any further than saying what I want, and what I tried to understand..

    @Edit
    I would say that what I need are the following information:
    -A calendar where I can get the information on how many days and hours are missing until the event
    -A timer that will start from that equation, so I can, for instance, reset the .jar and it'll still be counting the right time
    -Some sort of display that I can get the sentences in, the timer in the middle and some images on the background

    I just don't actually know anyway to do what I want...:X

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,948
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Need some help in a timer

    Your requirements:
    get the PCs time => See the System class method: currentTimeMillsec
    and will get the time left until a predetermined date => Set a Calendar class object to the date, get the time from that object and subtract the current time
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 25th, 2014)

  7. #5
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    I did this:
    Got the PC Time. CHECK
    Got today's date (could have set for the date I wanted). CHECK

    package timerCopa;
     
    import java.util.* ;
     
    public class Timer {
     
    	public static void main(String[] args) {
    		long tempoEmMs = System.currentTimeMillis() ;
    		System.out.println ("The time is: " + tempoEmMs);
     
    	GregorianCalendar today = new GregorianCalendar();
    	int day =today.get(GregorianCalendar.DAY_OF_MONTH);
    	int month =today.get(GregorianCalendar.MONTH) +1;
    	int year =today.get(GregorianCalendar.YEAR);
    	System.out.println ("The day of today is: " + day + "/" + month + "/" + year);
     
    	}
     
    }

    And am not sure on how to proceed.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,948
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Need some help in a timer

    What does the program do so far?
    What does it need to do next?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 25th, 2014)

  10. #7
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    It's not even executable yet (I guess)

    I need it to get the difference in days and hours until the date, for example "2 days and 21 hours" and print it big on the screen. That's the hardest part, I guess
    But I'm not sure how to make the interface, I would say that making an applet would be ok, but I don't know how to do it x;

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,948
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Need some help in a timer

    It compiles and executes OK for me.
    I need it to get the difference
    Get a Calendar object for the predetermined date, get its time and subtract the current time from that.
    That will give you the milliseconds from now to the predetermined date. Divide that out to get the time from now to predetermined date in the units you want. For example: Divide by 1000 to get the seconds, etc
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 25th, 2014)

  13. #9
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    package timerCopa;
     
    import java.util.* ;
     
    public class Timer {
     
    	public static void main(String[] args) {
    		long tempoEmMs = System.currentTimeMillis() ;
    		System.out.println ("The time is: " + tempoEmMs);
     
    	GregorianCalendar today = new GregorianCalendar();
    	int day =today.get(GregorianCalendar.DAY_OF_MONTH);
    	int month =today.get(GregorianCalendar.MONTH) +1;
    	int year =today.get(GregorianCalendar.YEAR);
    	System.out.println ("The day of today is: " + day + "/" + month + "/" + year);
     
    	GregorianCalendar cup = new GregorianCalendar();
    	cup.set(GregorianCalendar.HOUR_OF_DAY, 0);
    	cup.set(GregorianCalendar.DAY_OF_MONTH, 12);
    	cup.set(GregorianCalendar.MONTH, 6);
    	int dayCup = cup.get(GregorianCalendar.DAY_OF_MONTH);
    	int monthCup = cup.get(GregorianCalendar.MONTH);
    	int yearCup = cup.get(GregorianCalendar.YEAR);
    	long timeCup = (System.currentTimeMillis() - tempoEmMs);
    	System.out.println(timeCup);
     
    	}
     
    }

    Something went wrong... We are not 26 miliseconds away from the world cup... x;
    I think I checked the date wrongly...

  14. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,948
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Need some help in a timer

    The 26 ms is the time between the two calls to System.currentTimeMillis().
    The time in the GregorianCalendar object wasn't used for anything.
    The code needs to get the time from the GregorianCalendar object that is set for the future date of the event.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 25th, 2014)

  16. #11
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    And how to I tell the code that I want the time from that date?
    In this case, the timeCup variable.

  17. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,948
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Need some help in a timer

    There is a set() method (see the API doc for examples) that can be used to set the day, month, year, etc values in the GregorianCalendar object.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 25th, 2014)

  19. #13
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    package timerCopa;
     
    import java.util.* ;
     
    public class Timer {
     
    	public static void main(String[] args) {
    		long tempoEmMs = System.currentTimeMillis() ;
    		System.out.println ("The time is: " + tempoEmMs);
     
    	GregorianCalendar today = new GregorianCalendar();
    	int day =today.get(GregorianCalendar.DAY_OF_MONTH);
    	int month =today.get(GregorianCalendar.MONTH);
    	int year =today.get(GregorianCalendar.YEAR);
    	System.out.println ("The day of today is: " + day + "/" + month + "/" + year);
     
    	GregorianCalendar cup = new GregorianCalendar(2014,5,12);
    	long thisTimeInMs = cup.getTimeInMillis();
    	System.out.println ((thisTimeInMs - tempoEmMs) / 86400000) ;
    	int dayCup = cup.get(GregorianCalendar.DAY_OF_MONTH);
    	int monthCup = cup.get(GregorianCalendar.MONTH);
    	int yearCup = cup.get(GregorianCalendar.YEAR);
    	System.out.println ("Cup day: " + dayCup + "/" + monthCup + "/" + yearCup) ;
    	}
     
    }

    I think I got it. 48 days until the world Cup, and it's right.
    But I would like it to show the complete days and missing hours, at the moment it's missing aroung 48 days, 0 hours and 42 minutes, how can I display all that information?

    I would like some display in this way:
    Brjq1gb.png
    (it goes as 48 days, 0 hours, ...)
    That would count down.
    I could make the difference in days, but how to proceed from now on?
    Last edited by Kerooker; April 24th, 2014 at 10:23 PM.

  20. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,948
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Need some help in a timer

    If you have the code for computing the time left until the day, now you need the timer that will update the time left every so often and show it.
    First do the timer
    then do the display
    A Swing Timer will need an ActionListener object to call every interval. See the API doc for an example.
    In the actionPerformed() method compute the time remaining and display it.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 25th, 2014)

  22. #15
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    Trying to create a timer failed as I probrably failed to import the right packages, it's not recognizing ActionListener as something..


    package timerCopa;
     
    import java.util.GregorianCalendar;
    import javax.* ;
     
    public class Timer {
     
    	public static void main(String[] args) {
    		long tempoEmMs = System.currentTimeMillis() ;
    		System.out.println ("The time is: " + tempoEmMs);
     
    	GregorianCalendar today = new GregorianCalendar();
    	int day =today.get(GregorianCalendar.DAY_OF_MONTH);
    	int month =today.get(GregorianCalendar.MONTH);
    	int year =today.get(GregorianCalendar.YEAR);
    	System.out.println ("The day of today is: " + day + "/" + month + "/" + year);
     
    	GregorianCalendar cup = new GregorianCalendar(2014,5,12);
    	long thisTimeInMs = cup.getTimeInMillis();
    	System.out.println ((thisTimeInMs - tempoEmMs) / 86400000) ;
    	int dayCup = cup.get(GregorianCalendar.DAY_OF_MONTH);
    	int monthCup = cup.get(GregorianCalendar.MONTH);
    	int yearCup = cup.get(GregorianCalendar.YEAR);
    	System.out.println ("Cup day: " + dayCup + "/" + monthCup + "/" + yearCup) ;
     
    	//timer attempt 1 //	
    	ActionListener listener = new ActionListener(){
    		  public void actionPerformed(ActionEvent event){
    		    System.out.println("test1");
    		  }
    		};
    		Timer displayTimer = new Timer(1000, listener);
    		displayTimer.start();
    	}
     
    }

    Oh, btw, i'm not sure if it's alright for me to ask all these things..

  23. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,948
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Need some help in a timer

    failed to import the right packages,
    The import statement needs ALL of the subpackage names up to the classname. The classname can be included(post#15) or an * used(post#13) after all the subnames to reference all classes in that "folder".

    it's alright for me to ask all these things.
    Yes, it is alright. These are common beginner questions and problems.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 25th, 2014)

  25. #17
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    Using import gives me 2 errors: 1- a conflict with GregorianCalendar, I guess, and 2- it still doesn't recognize ActionListener as a type
    import javax.swing.Timer ;

    @Edit
    Tweaking around, I've made this:

    package timerCopa;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.GregorianCalendar;
    import javax.swing.* ;
     
    public class Timer {
     
    	public static void main(String[] args) {
    		long tempoEmMs = System.currentTimeMillis() ;
    		System.out.println ("The time is: " + tempoEmMs);
     
    	GregorianCalendar today = new GregorianCalendar();
    	int day =today.get(GregorianCalendar.DAY_OF_MONTH);
    	int month =today.get(GregorianCalendar.MONTH);
    	int year =today.get(GregorianCalendar.YEAR);
    	System.out.println ("The day of today is: " + day + "/" + month + "/" + year);
     
    	GregorianCalendar cup = new GregorianCalendar(2014,5,12);
    	long thisTimeInMs = cup.getTimeInMillis();
    	System.out.println ((thisTimeInMs - tempoEmMs) / 86400000) ;
    	int dayCup = cup.get(GregorianCalendar.DAY_OF_MONTH);
    	int monthCup = cup.get(GregorianCalendar.MONTH);
    	int yearCup = cup.get(GregorianCalendar.YEAR);
    	System.out.println ("Cup day: " + dayCup + "/" + monthCup + "/" + yearCup) ;
     
    	//timer attempt 1 //	
    	ActionListener listener = new ActionListener(){
    		  public void actionPerformed(ActionEvent event){
    		    System.out.println("test'1");
    		  }
    		};
    		Timer displayTimer = new Timer(1000, listener);
    		displayTimer.start();
    	}
     
    }

    But it wouldn't recognize listener as part of the constructor, nor start(); as a method for Timer.
    Last edited by Kerooker; April 25th, 2014 at 10:04 PM.

  26. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,948
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Need some help in a timer

    it wouldn't recognize listener as part of the constructor
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  27. #19
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    firstly, at Timer displayTimer = new Timer (1000, listener); I get the error "The constructor Timer(int, ActionListener) is undefined"
    After that, I get an error at displayTimer.start(); "The method Start() is undefined for type Timer.

  28. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,948
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Need some help in a timer

    "The constructor Timer(int, ActionListener) is undefined"
    Your class is named Timer. That is where the compiler is looking.

    It is ALWAYS a BAD IDEA to use java SE class names. Do something like using unique names by adding My as a prefix.
    If you don't understand my answer, don't ignore it, ask a question.

  29. #21
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    	ActionListener listener = new ActionListener(){
    		  public void actionPerformed(ActionEvent event){
    		    System.out.println("test'1");
    		  }
    		};
    		myTimer displayTimer = new Timer(1000, listener);
    		displayTimer.start();

    Still won't work, at line myTimer displayTimer = new Timer(1000, listener); The constructor Timer(int, ActionListener) is undefined
    and at the same line, myTimer cannot be resolved to a type

  30. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,948
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Need some help in a timer

    What is the name of your class that this code is inside of? See post#20

    The new code using Timer in post#17 was probably OK.
    If you don't understand my answer, don't ignore it, ask a question.

  31. #23
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    Oh, now I got it.
    package timerCopa;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.GregorianCalendar;
    import javax.swing.* ;
     
    public class myTimer {
     
    	public static void main(String[] args) {
    		long tempoEmMs = System.currentTimeMillis() ;
    		System.out.println ("The time is: " + tempoEmMs);
     
    	GregorianCalendar today = new GregorianCalendar();
    	int day =today.get(GregorianCalendar.DAY_OF_MONTH);
    	int month =today.get(GregorianCalendar.MONTH);
    	int year =today.get(GregorianCalendar.YEAR);
    	System.out.println ("The day of today is: " + day + "/" + month + "/" + year);
     
    	GregorianCalendar cup = new GregorianCalendar(2014,5,12);
    	long thisTimeInMs = cup.getTimeInMillis();
    	System.out.println ((thisTimeInMs - tempoEmMs) / 86400000) ;
    	int dayCup = cup.get(GregorianCalendar.DAY_OF_MONTH);
    	int monthCup = cup.get(GregorianCalendar.MONTH);
    	int yearCup = cup.get(GregorianCalendar.YEAR);
    	System.out.println ("Cup day: " + dayCup + "/" + monthCup + "/" + yearCup) ;
     
    	//timer attempt 1 //	
    	ActionListener listener = new ActionListener(){
    		  public void actionPerformed(ActionEvent event){
    		    System.out.println("test'1");
    		  }
    		};
    		Timer displayTimer = new Timer(1000, listener);
    		displayTimer.start();
    	}
     
    }

    Will keep working.

  32. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,948
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Need some help in a timer

    OK, next try computing the duration from the current time to an event a short time in the future and have the actionPerformed method print out a countdown message showing the time left.

    One problem you may have is when the main() method exits and the JVM doesn't find any important threads executing and it will end the program.
    This can be handled in the test code by using the Thread sleep() method at the end of main() to stop main() from exiting while the timer is running.
    Something like this:
           Thread.sleep(100000);     //  Let timer run
    If you don't understand my answer, don't ignore it, ask a question.

  33. #25
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    I'm having problems with the timer; It won't print "Test1" in the console every second...I can't keep experimenting with other times if the basic one won't work x;

Page 1 of 4 123 ... LastLast

Similar Threads

  1. Swing TIMER
    By newtolearningjava in forum What's Wrong With My Code?
    Replies: 14
    Last Post: April 13th, 2014, 05:01 PM
  2. [SOLVED] both class javax.swing.Timer in javax.swing and class java.util.Timer in java.util match
    By stresstedout in forum What's Wrong With My Code?
    Replies: 13
    Last Post: April 10th, 2014, 08:32 PM
  3. Countdown timer
    By AANZ in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 1st, 2013, 08:46 AM
  4. Ticked Off Timer
    By JavaCow in forum Loops & Control Statements
    Replies: 8
    Last Post: October 30th, 2010, 10:18 AM
  5. Timer?
    By TimW in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 27th, 2009, 08:43 AM