开发者

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 ty开发者_开发百科pe 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.


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)


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜