Results 1 to 6 of 6
  1. #1
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default How to extract a date from a JTable

    One element I load to a JTable is a date, once it is there how do I extract the contents so that I can work with it as a date?


    // Here are the dates
    Date date1 = new GregorianCalendar(2009,0,26).getTime();
    Date date2 = new GregorianCalendar(2008,2,20).getTime();

    // Right now they are in an array, eventually they will come from a file
    private Object[][] serviceTable = {
    { 97300, date1, "McElhinney's Service", 4798, "Check alternator and oil pressure", "Battery, Alternator, Serpentine Belt, Oil Pressure Sensor", 707.68},
    { 0, date2, "McElhinney's Service", 11112, "Replace engine", "Motor, cables, fluids", 4534.61}
    };


    private JTable table;

    // Load the array into a table
    private void PopulateTable()
    {
    for ( int r = 0; r < serviceTable.length; r++ )
    for ( int c = 0; c < serviceTable[r].length; c++ )
    model.setValueAt(serviceTable[r][c], r, c);
    }


    Later on in the code, depending on input from the user, I am going to want to extract the date that is in model.getValueAt(r,1), which is the date. I can't find anything that converts an object into a Date (either Date or Calendar).

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    I can't find anything that converts an object into a Date (either Date or Calendar).
    As you where told in your last posting you can't just make an Object be anything your want. That is, you add a Date to the model you can't just magically make it become a Calendar. That is why you where told you need to create a custom renderer if you want to store a Calendar in the model.

    However, since it "is a" Date then you can "cast" it to a Date:

    Java Code:
    Date value = (Date)model.getValueAt(...);

  3. #3
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    camickr, I did create a custom renderer to display the date on the table. The is the back end of it, where I am trying to extract the information from the table. I was able to extract all of the other data in the table (strings, ints, doubles). And, I could extract the date as a string, but I was having problems finding a way to extract it as a date.

  4. #4
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    When I was looking for ways to extract this, I came across parsing using SimpleDateFormatter. Is this now the preferred way to extract dates from different sources? Any thoughts (pros/cons) on using SimpleDateFormatter?

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Only one question per thread. Your original question has been answered. If you want to know about formatting of Date objects then ask a new question. It gets too confusing when multiple questions and multiple answers are given in the same thread.

  6. #6
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

Similar Threads

  1. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 08:10 AM
  2. Replies: 2
    Last Post: 08-12-2008, 12:26 PM
  3. how to extract the date n time from images
    By norazanita in forum New To Java
    Replies: 3
    Last Post: 06-20-2008, 05:45 PM
  4. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 08:32 PM
  5. Difference between current date and anothe date
    By vijay balusamy in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 04:15 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
  •