Results 1 to 11 of 11
  1. #1
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Displaying Date and Time in GUI

    I have read some tutorials on the Data Class, and have come up with the following code:

    Java Code:
    Date dNow = new Date( );
          SimpleDateFormat ft = 
          new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
    
          System.out.println("Current Date: " + ft.format(dNow));
    It displays the following Code:

    Current Date: Tue 2013.06.04 at 04:37:45 PM EDT

    I am using the NetBeans GUI and trying to get the data and time to display in a text field. I am having a hard time with this.

    Java Code:
     txtDate.setText("E yyyy.MM.dd");
    I am getting <identifier> expected errors. I can't figure out how to convert it to a string either.

  2. #2
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Displaying Date and Time in GUI

    Java Code:
    Date dNow = new Date( );
          SimpleDateFormat ft = 
          new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
    
          
         String fun = ft.format(dNow);
         txtDate.setText(fun);
    Can convert to string now, but still getting identifier errors when I try to display it.

  3. #3
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Displaying Date and Time in GUI

    For some reason, if I hit a button, it will function. I could not get the time and date to display without hitting this button. How do I go about getting the time to tick every second?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Displaying Date and Time in GUI

    Quote Originally Posted by no_slip_condition View Post
    For some reason, if I hit a button, it will function. I could not get the time and date to display without hitting this button. How do I go about getting the time to tick every second?
    Use a Swing Timer to get the latest time and to set the JTextField with its String representation.

  5. #5
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Displaying Date and Time in GUI

    Hmm, so use the timer class to get the time every second? I've never used the timer class.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Displaying Date and Time in GUI

    Quote Originally Posted by no_slip_condition View Post
    Hmm, so use the timer class to get the time every second? I've never used the timer class.
    Yep, makes sense, doesn't it? A Timer calls your code every xxx milliseconds. Using it is easy and is well described here: Swing Timer Tutorial.

  7. #7
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Displaying Date and Time in GUI

    I'm having a hard time with the syntax of the timer...

    Java Code:
    import java.util.Scanner;
    import java.text.DecimalFormat;
    import java.util.Date;
    
    import javax.swing.Timer;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    public class JavaApplication45 {
    
        
        public static void main(String[] args) 
        {
        
        
          
    }
    }
    Timer newTimer = new Timer(); causes error, it obviously doesn't behave like the Scanner class. The code they give in the tutorial is for some Applet.

    Java Code:
    Timer(int, ActionListener)
    I have seen this code as mentioned as initializing it, but what is ActionListener? Is that the operation I what performed, or is ActionListerner a method in my Class?

  8. #8
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Displaying Date and Time in GUI

    Java Code:
        
        public static void main(String[] args) 
        {
        Date dNow = new Date( );
        SimpleDateFormat time = new SimpleDateFormat ("hh:mm:ss a");
          
         ActionListener listener = new ActionListener(){
      public void actionPerformed(ActionEvent event){
        System.out.println("hello");
      }
    };
    Timer displayTimer = new Timer(1000, listener);
    
    displayTimer.start();
    }
    }
    Feellike I am getting closer, but nothing is displayed in the output. What is wrong here?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Displaying Date and Time in GUI

    It's hard to tell what your code is doing since the formatting makes it hard to read. You know that if you format correctly with blocks lining up and with regular indentation, others can read and understand your code much better. That's what formatting is all about, especially if you're asking others to help you.

    As for your code, I don't see where you're creating and showing a Swing GUI, so wit nothing to make the Swing thread to run, it looks like the program should and will exit promptly.

  10. #10
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Displaying Date and Time in GUI

    I figured it out. Turns out that output only displays in the GUI, I was testing it outside the GUI. Of course, being new to programming, theres no way I could have ever known that. Thanks for your help.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Displaying Date and Time in GUI

    The output will display on the command line, but only if your program creates and displays a GUI, any GUI, in order to prevent the Swing thread (and the whole program) from exiting immediately.

Similar Threads

  1. Replies: 8
    Last Post: 02-26-2013, 06:41 PM
  2. Replies: 18
    Last Post: 05-11-2011, 08:36 AM
  3. Question for Displaying time
    By siva_vs_siva in forum New To Java
    Replies: 5
    Last Post: 10-29-2010, 09:22 AM
  4. converting string (GMT date) to date in US time.
    By JRuyechan in forum New To Java
    Replies: 1
    Last Post: 10-15-2010, 08:07 AM
  5. Displaying date
    By Manfizy in forum New To Java
    Replies: 2
    Last Post: 07-14-2009, 11:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •