Results 1 to 6 of 6
- 06-16-2010, 07:58 PM #1
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).
- 06-16-2010, 10:00 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.I can't find anything that converts an object into a Date (either Date or Calendar).
However, since it "is a" Date then you can "cast" it to a Date:
Java Code:Date value = (Date)model.getValueAt(...);
- 06-16-2010, 10:16 PM #3
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.
- 06-17-2010, 03:01 AM #4
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?
- 06-17-2010, 03:18 AM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 06-17-2010, 10:28 AM #6
Similar Threads
-
Compare date input to database with current date
By hungleon88 in forum Advanced JavaReplies: 2Last Post: 11-25-2008, 08:10 AM -
[SOLVED] Update jTable with new data (especially Date value Format)
By gustio in forum New To JavaReplies: 2Last Post: 08-12-2008, 12:26 PM -
how to extract the date n time from images
By norazanita in forum New To JavaReplies: 3Last Post: 06-20-2008, 05:45 PM -
Creating a Gregorian Calendar using a Date object gives date - 1
By prachi_goliwadekar in forum New To JavaReplies: 1Last Post: 05-08-2008, 08:32 PM -
Difference between current date and anothe date
By vijay balusamy in forum New To JavaReplies: 1Last Post: 04-16-2008, 04:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks