• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Displaying date information in JTable

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm looking to import date information in a column of a JTable. The date is being read from a database as a long variable (milliseconds from 1970). I've set the date column to be of type 'Date'. My question is this: what's the best way to convert the <code>long</code> variable into a <code>Date</code> variable? I could iterate through all the rows and convert the long to Date before saving the data to the table model. Might this be an expensive operation if I have to iterate over thousands of rows? Or...could I override the <code>getValueAt</code> method in the DefaultTableModel and convert the long to a Date variable at that point? Or am I making this too difficult and there's a simple solution I've missed?

Thanks for the input,
Dave

[ February 03, 2005: Message edited by: David Irwin ]
[ February 03, 2005: Message edited by: David Irwin ]
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if this will work for you, but both java.util.Calendar and java.util.GregorianCalendar have a method called setTimeInMillis

setTimeInMillis

public void setTimeInMillis(long millis)

Sets this Calendar's current time from the given long value.

Parameters:
millis - the new time in UTC milliseconds from the epoch.


I just don't remember if that definition "milliseconds from the epoch" means Jan 1, 1970 or not.


Then, both classes have getTime, which returns a Date:

getTime

public final Date getTime()

Gets this Calendar's current time.

Returns:
the current time.


So you'd create a Calendar or GregorianCalendar, setTimeInMillis to the value from the database, then getTime to get a Date.

I haven't needed to use these myself, but will this work for your needs?

I don't know about the performance aspects of doing this thousands of times, though, before displaying the table. That might be problematic.

Maybe doing a custom cell renderer/editor would be easier? I've unfortunately minimal (ie: almost none) experience in doing that.
 
David Irwin
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joe for the response.

I'm familiar with the long -> Date conversion but what I'm hoping to do is the following:

-set the a column's class to a 'Date' object
-then save the date information (in milliseconds from epoch) in a table model
-and then have the JTable display the date formatted to something like '05-02-03 11:30:00' without having to explicitly do any long -> Date conversions.

My question comes down to whether something exists in the JTable that automatically does this or do I need to explicitly do the long -> Date conversion myself.

Thanks again.
[ February 03, 2005: Message edited by: David Irwin ]
 
Joe Vahabzadeh
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I see.

Um, once again, my mega-disclaimer of NOT being an expert at tables.....

But according to this page, there doesn't seem to be a default cell-editor to do that. (see about halfway down the page regarding editors/renderers).

It looks kind of like you'll have to create your own cell renderer and editor in a table model of your own design, and have all the dirty work done there.

On the other hand, from a user perspective, using a custom editer and renderer won't seem as slow as the initial lag that may occur if you have to convert thousands of them before displaying.

Even if you did convert them all to Date before displaying, unless you're happy with the format in short date style format, you'd still have to do your own renderer and editor.

[ February 03, 2005: Message edited by: Joe Vahabzadeh ]
[ February 03, 2005: Message edited by: Joe Vahabzadeh ]
 
Their achilles heel is the noogie! Give them noogies tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic