user393964
user393964

Reputation:

Timespan in Java and SQL?

I want to save time intervals in my SQL table but I'm not sure what type to use in Java for this.

What I need to store are durations like 12:33:57 or just minutes and seconds like 02:19. Any ideas what type I could use for java and for sql? I don't really need to make any calculations with these values, but I would like to generate charts later on, with the lengths of the mesured times.

Upvotes: 3

Views: 794

Answers (2)

Lucas
Lucas

Reputation: 885

You could store the durations as seconds (or milliseconds) in an int field in Java and in an integer column in SQL (e.g. 5 minutes and 12 seconds would be stored as 312).

The Java int field could be easily converted to any format necessary for your charts (if int isn't already the best format).

Upvotes: 0

Bohemian
Bohemian

Reputation: 424983

The mysql type you want is TIME, ie:

create table mytable (
  ...
  my_interval TIME,
  ...
);

If you read such columns in via JDBC, you'll get a java.sql.Time object, which is actually a java.util.Date with only the time portion set (ie a time on 1970-01-01)

Upvotes: 2

Related Questions