• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

addition and subtraction of time

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how do you calculate time in java ,to get elapsed and remaining from some initial time
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Points in time in Java are usually rempresented by Date objects. A Date object is ultimately a duration of milliseconds since the "epoch" (1/1/1970 00:00:00). Assuming you have your end point and your start point, it is a simple case of subtracting one from the other.
 
Alex Rugav
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got the errorr:cannot apply - operator to java.util.Date
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot apply the + or - operators directly to Objects (with the special case of the String class). But you can apply them to primative long values. Now you just need a way of getting the long value (i.e. the number of milliseconds since the epoch) that your Date objects encapsulates. If you read the JavaDocs for the Date class, you should be able to spot how to do that.
 
Alex Rugav
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
would u kindly help with a piece of code for that!
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, that's not really what these forums are for. JavaRanch's intention is to help people learn how to do things for themselves, not do work for them.

Look at the JavaDocs for java.util.Date. Do you see any methods that get, oh, the time from a Date object as a long value?
[ May 26, 2006: Message edited by: Paul Sturrock ]
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's possible that you (Alex) didn't realize that all of the standard Java classes are comprehensively documented and that the documentation is available on the internet. Here is a link:

http://java.sun.com/j2se/1.5.0/docs/api/index.html
 
please buy my thing and then I'll have more money:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic