• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Storing and measuring duration?

 
Ranch Hand
Posts: 89
Netscape MySQL Database Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 76406
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76406
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:

and I think it will work.
 
Just the other day, I was thinking ... about this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic