Search code examples
javalocaldate

Get difference of seconds beetween two dateTime


How can I calculate the difference in seconds between two dates?

I have this:

LocalDateTime now = LocalDateTime.now(); // current date and time
LocalDateTime midnight = now.toLocalDate().atStartOfDay().plusDays(1); //midnight

In this case the time is: now 2017-09-14T09:49:25.316 midnight 2017-09-15T00:00

How i calculate int second = ...?

And the result, in this case, that i want return is 51035

How i can do?

UPGRADE SOLVED

I try this:

DateTime now = DateTime.now();
DateTime midnight = now.withTimeAtStartOfDay().plusDays(1);
Seconds seconds = Seconds.secondsBetween(now, midnight);
int diff = seconds.getSeconds();

Now return the difference beetween the date in seconds in integer variable.

Thank all user for response.


Solution

  • int seconds = (int) ChronoUnit.SECONDS.between(now, midnight);