aspose file tools*
The moose likes Object Relational Mapping and the fly likes How do I implement time? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How do I implement time?" Watch "How do I implement time?" New topic
Author

How do I implement time?

Mike Fitzgerald
Greenhorn

Joined: Dec 03, 2011
Posts: 3
I want to create a list of appointments. Order them by the current time. I've created a time data type in my JDBC database, however I'm unsure how to input times into this datatype. I can't find anything online either. All the examples seem to only deal with inserting dates.

I only need time though, things happen the same time every day. The actual date is of no importance to me. If someone can steer me in the right direction to read up on this I'd be very thankful. At present I'm very lost! I'm thinking maybe I should just make a String datatype called time and perform validation on it to ensure its between 0000 and 2359 that system actually seems simpler at the moment.
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 157

Mike Fitzgerald wrote:I want to create a list of appointments. Order them by the current time. I've created a time data type in my JDBC database, however I'm unsure how to input times into this datatype. I can't find anything online either. All the examples seem to only deal with inserting dates.

I only need time though, things happen the same time every day. The actual date is of no importance to me. If someone can steer me in the right direction to read up on this I'd be very thankful. At present I'm very lost! I'm thinking maybe I should just make a String datatype called time and perform validation on it to ensure its between 0000 and 2359 that system actually seems simpler at the moment.

Since you spoke about JDBC, I'm not sure whether or not you are actually using Java EE with a persistence manager and the like.

Anyway, if you are using a persistence provider (like Hibernate), you should put the @Temporal annotation on your time field on your entity class.


"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Mike Fitzgerald
Greenhorn

Joined: Dec 03, 2011
Posts: 3
Ah, sorry forgot to mention that bit.

Yea I'm working in J2EE using NetBeans with the GlassFish server.

Connected a JDBC database to my web application using Hibernate for persistence. Everything was fine until I tried to implement time. I just can't figure out how it's set, or what value I have to set in as time. All the examples online seem to relate to date. All I want is a time such as 20:30 or 16:30, the rest of the data from java.util.date is useless to me.

Will I just have to set junk data in for the rest?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
What a tedious practical subject. When I saw the title I expected something like an appendix to Genesis chapter 1.
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 157

Mike Fitzgerald wrote:Ah, sorry forgot to mention that bit.

Yea I'm working in J2EE using NetBeans with the GlassFish server.

Connected a JDBC database to my web application using Hibernate for persistence. Everything was fine until I tried to implement time. I just can't figure out how it's set, or what value I have to set in as time. All the examples online seem to relate to date. All I want is a time such as 20:30 or 16:30, the rest of the data from java.util.date is useless to me.

Will I just have to set junk data in for the rest?

Look at the link I posted; there is a time type. Also look at the Calendar class. That will allow you to extract a time from a date.
Mike Fitzgerald
Greenhorn

Joined: Dec 03, 2011
Posts: 3
Ah, sorry didn't notice that was a link earlier.

Anyway, it kind of resolves my issue though I'm still unclear as how the value I load into the database is supposed to be formated. Like when a user would insert the value via a CRUD, what would they enter? I know this seems like a bafflingly stupid question, but I'm curious.. I've never actually had to work with time or databases for that method. Only time related things I've done was implementing a timer for java games.
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 157

Mike Fitzgerald wrote:Anyway, it kind of resolves my issue though I'm still unclear as how the value I load into the database is supposed to be formated. Like when a user would insert the value via a CRUD, what would they enter?

If you're going through Hibernate you don't have to worry about the format of the value - it will be a java.util.Date instance in your entity class and Hibernate will handle the mapping to a SQL time type.

If you were to generate a SQL query from a simple CRUD app you could just put the time in an 'hh:mm' format in your query; e.g., UPDATE MyTable SET myTime = '12:00'.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I implement time?