开发者

How to find out seconds between two dates in java [duplicate]

开发者 https://www.devze.com 2023-03-25 16:40 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: In Java, how do I get the difference in seconds between 2 dates?
This question already has answers here: Closed 11 years ago.

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 d开发者_运维技巧ifference between two above dates.


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.


  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.
0

精彩评论

暂无评论...
验证码 换一张
取 消