-3

Possible Duplicate:
In Java, how do I get the difference in seconds between 2 dates?

Hi i have two dates as follows

1970-01-01 and 2011-08-01 which are in yyyy-mm-dd format.

How to find out the seconds difference between two above dates.

Community
  • 1
  • 1
  • are you only get the seconds from the epoch to the current data every time? If so you can just call `System.currentTimeMillis() / 1000` – Hunter McMillen Aug 01 '11 at 20:25

2 Answers2

3

Create two java.util.Date objects (yes, the deprecated ones).

Use the getTime() method on each object to get milliseconds from the epoch.

Subtract the smaller time from the larger time, and then convert to seconds by dividing by one thousand.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
2
  1. Convert both to dates using SimpleDateFormat.
  2. Get the difference between them by calling getTime() and subtracting one from the other.
  3. Divide the number by 1000 to get seconds.
nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120