• Post Reply Bookmark Topic Watch Topic
  • New Topic

addition and subtraction of time  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do you calculate time in java ,to get elapsed and remaining from some initial time
 
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
i got the errorr:cannot apply - operator to java.util.Date
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
would u kindly help with a piece of code for that!
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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 ]
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!