Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I implement time?

 
Mike Fitzgerald
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Fitzgerald
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2851
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What a tedious practical subject. When I saw the title I expected something like an appendix to Genesis chapter 1.
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic