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

    Default datetime on a jtextfield

    [This is a multi-post; I thank you people for the attention]

    hi

    How could I show a database datetime field (like mm/dd/yyyy) on a JTextField?

    regards

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    The JTextField works with a String right, setText().
    where a database datetime is going to be a java.util.Date (or a java.sql.Date, or a java.sql.Timestamp)

    The java.text.SimpleDateFormat is handy for converting between strings and dates. You specify the formatting string, see the javadoc

    Where in this example I assume we have read a timestamp object,
    Java Code:
    SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yyyy");
    Timestamp myDatabaseTime = // fetched from database right...
    java.util.Date aDate = new java.util.Date(myDatabaseTime.getTime()); // not even required to convert it, timestamp extends Date.
    
    jTextField.setText(fmt.format(aDate));

Similar Threads

  1. JTextField
    By gancio in forum AWT / Swing
    Replies: 20
    Last Post: 08-26-2009, 03:11 PM
  2. Replies: 2
    Last Post: 08-05-2009, 11:00 AM
  3. Replies: 1
    Last Post: 01-30-2009, 06:44 PM
  4. JtextField
    By kashifu in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2008, 04:25 PM
  5. help with JTextfield
    By gary in forum New To Java
    Replies: 4
    Last Post: 07-11-2007, 01:58 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
  •