Results 1 to 6 of 6
  1. #1
    BluXit is offline Member
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Default How to display date & time on GUI?

    Just want to know how to display it? Thanks

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

    Default

    1) Get today's date and Convert to String -- SimpleDateFormat can help with conversion.
    2) Display in JLabel -- Swing tutorials will help you with this.

    If you run into any specific problems with your attempt, please show your code and let us know what and where the problems are.

    Much luck!

  3. #3
    BluXit is offline Member
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Default

    How to use SimpleDateFormat?
    Can you give me an example?
    I'm already know to use JLabel btw. Thanks. :D

  4. #4
    BluXit is offline Member
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Default

    Okay I found out how to use SimpleDateFormat.
    But now when in JLabel it gives me an error.
    I wrote the code like this
    Java Code:
    	GregorianCalendar calendar = new GregorianCalendar();
    	Date date = new Date();
    	Date now = calendar.getTime();
        
       JLabel lbl1 = new JLabel(now);

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by BluXit View Post
    Okay I found out how to use SimpleDateFormat.
    But now when in JLabel it gives me an error.
    I wrote the code like this
    Java Code:
    	GregorianCalendar calendar = new GregorianCalendar();
    	Date date = new Date();
    	Date now = calendar.getTime();
        
       JLabel lbl1 = new JLabel(now);
    The JLabel doesn't take a Date as a parameter for its constructor; you could've known this had you read the API documentation (<--- hint). Also read the API docs for the Date class, it has a nive method to convert it to a String, but you already said you're using the SimpleDateFormat class?

    kind regards,

    Jos

  6. #6
    thomasdevos's Avatar
    thomasdevos is offline Member
    Join Date
    Feb 2010
    Posts
    19
    Rep Power
    0

    Default

    hi BluXit,
    take it, run it, you know it

    Java Code:
    import java.awt.Dimension;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class DateShowAPI {
    	public static void main(String[] args) {
    		JFrame frame = new JFrame();
    		frame.setTitle("the title");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setPreferredSize(new Dimension(800, 600));
    		
    		String date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
    		JLabel l = new JLabel(date);
    		
    		frame.getContentPane().add(l);
    		
    		frame.pack();
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    
    	}
    }
    Thomas Devos

    Quote Originally Posted by BluXit View Post
    Just want to know how to display it? Thanks
    i hold 7 years develop exp. now i start a thread to share my knowlege about a j2ee project. welcome to participate.Study Java Through Real Java Project

Similar Threads

  1. get date, time from jSpinner
    By cowboy in forum New To Java
    Replies: 3
    Last Post: 12-08-2009, 01:32 PM
  2. Time and Date
    By Manfizy in forum New To Java
    Replies: 0
    Last Post: 05-27-2009, 01:58 PM
  3. Date n Time Display problem
    By kapilverma32 in forum Advanced Java
    Replies: 6
    Last Post: 02-10-2009, 02:53 PM
  4. Time and Date in Java
    By java_fun2007 in forum New To Java
    Replies: 4
    Last Post: 11-06-2007, 08:25 PM
  5. how to get the current date and time
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 07:05 PM

Posting Permissions

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