Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Storing and measuring duration?  RSS feed

 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you read time already spend on work from a database you get it in java.sql format (e.g. "01:15:00"). How can I store this 5 quarters of an hour into a variable, next measure time spend on work, add it to the stored 5 quarters of an hour, and write this time to the database?

I'm looking at Period and Duration but see no possible ways to store time already spend in a local variable.

What I would like to have is something like:

I can get the time spend from the database, the 'project.getTime()' part. I can not assign this result to a local variable (the 'timespend =' part).

Do you convert the time already spend ("01:15:00") to some kind of long, and later on translate it back to SQL time?
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that Duration and most of the newer date‑time classes are immutable. You cannot therefore write
myDuration.add(otherDuration);
You would have to write
myDuration = myDuration.add(otherDuration);
Duration has a static factory method called between which will probably take your start and stop Instants as arguments.

I don't know how you should convert them from local variables to fields and vice versa. You would use the normal techniques for setting and getting fields.
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't remember how to turn your SQL text into a duration. I am sure the SQL doesn't actually return text. How is it actually stored? If it is stored as seconds/ms/μs try the Duration#of methods.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daan Heuvelbeuk wrote:If you read time already spend on work from a database you get it in java.sql format (e.g. "01:15:00").

No you don't - unless you stored it that way - you should get a java.sql.Time object, which is simply a wrapper to a java.util.Date. How you get from that to a Duration, I'm not quite sure - haven't boned up on v8 enough yet - but I suspect it's fairly straightforward.

Don't confuse what an object IS with what it looks like when you display it.

Winston
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:

and I think it will work.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!