Skip to main content
added 3711 characters in body
Source Link
Thomas
  • 88.9k
  • 13
  • 126
  • 162

Duration d = Duration.between(fromDateTime, toDateTime); long days = d.toDays(); long seconds = d.toSeconds();

Duration d = Duration.between(fromDateTime, toDateTime); long days = d.toDays(); long seconds = d.toSeconds(); 

long days = ChronoUnit.DAYS.between(fromDateTime, toDateTime); long seconds = ChronoUnit.SECONDS.between(fromDateTime, toDateTime);

long days = ChronoUnit.DAYS.between(fromDateTime, toDateTime); long seconds = ChronoUnit.SECONDS.between(fromDateTime, toDateTime); 
long days = fromDateTime.until(toDateTime, ChronoUnit.DAYS);long days = fromDateTime.until(toDateTime, ChronoUnit.SECONDS);
long days = fromDateTime.until(toDateTime, ChronoUnit.DAYS); long days = fromDateTime.until(toDateTime, ChronoUnit.SECONDS); 

Difference between 2 timestamps in a multiple parts with different units

It's "semi-manual" because you'll need to write some form of algorithm to get order right (the manual part) but can still use read-to-use calculations like plusXxx() and until() (the automatic part).

Duration d = Duration.between(fromDateTime, toDateTime); long days = d.toDaysPart(); long seconds = d.toSecondsPart();

Duration d = Duration.between(fromDateTime, toDateTime); long days = d.toDaysPart(); long seconds = d.toSecondsPart(); 
  • Units above days are not supported as of Java 21
  • Units in between will be missed, i.e. the example above will return 10859 days from toDaysPart() but only 50 seconds from toSecondsPart() so you'd miss 22 hours and 54 minutes in between. Using Temporal.until() as in my original answer gives you the ability to express the example duration as "10859 days and 82490 seconds"

Manual calculations

A final option that _may_ be faster than the others(albeit probably very negligible) would be to calculate everything yourself.

The basic idea is to get the milliseconds between the 2 dates and then do your calculations. For sake of simplicity I'll use Duration.toMillis() but there are other options as well which might be faster.

 long millis = Duration.between(fromDateTime, toDateTime).toMillis(); long millisPerDay = 24 * 60 * 60 * 1000; //24 hours * 60 minutes * 60 seconds * 1000 millis long days = millis / millisPerDay; //example yields 10859 long seconds = millis / 1000; //example yields 938300090 

Notes:

  • This may be the fastest calculation and be sufficient for simple use cases
  • If you're using larger units like months or years you may need to take length differences into account (months range from 28 to 31 days, years from 365 to 366 days, etc.) which would make this more complex and especially error prone

Duration d = Duration.between(fromDateTime, toDateTime); long days = d.toDays(); long seconds = d.toSeconds();

long days = ChronoUnit.DAYS.between(fromDateTime, toDateTime); long seconds = ChronoUnit.SECONDS.between(fromDateTime, toDateTime);

long days = fromDateTime.until(toDateTime, ChronoUnit.DAYS);long days = fromDateTime.until(toDateTime, ChronoUnit.SECONDS);

Difference between 2 timestamps in a multiple parts with different units

Duration d = Duration.between(fromDateTime, toDateTime); long days = d.toDaysPart(); long seconds = d.toSecondsPart();

  • Units above days are not supported as of Java 21
  • Units in between will be missed, i.e. the example above will return 10859 days from toDaysPart() but only 50 seconds from toSecondsPart() so you'd miss 22 hours and 54 minutes in between. Using Temporal.until() as in my original answer gives you the ability to express the example duration as "10859 days and 82490 seconds"
Duration d = Duration.between(fromDateTime, toDateTime); long days = d.toDays(); long seconds = d.toSeconds(); 
long days = ChronoUnit.DAYS.between(fromDateTime, toDateTime); long seconds = ChronoUnit.SECONDS.between(fromDateTime, toDateTime); 
long days = fromDateTime.until(toDateTime, ChronoUnit.DAYS); long days = fromDateTime.until(toDateTime, ChronoUnit.SECONDS); 

Difference between 2 timestamps in multiple parts with different units

It's "semi-manual" because you'll need to write some form of algorithm to get order right (the manual part) but can still use read-to-use calculations like plusXxx() and until() (the automatic part).

Duration d = Duration.between(fromDateTime, toDateTime); long days = d.toDaysPart(); long seconds = d.toSecondsPart(); 
  • Units above days are not supported as of Java 21
  • Units in between will be missed, i.e. the example above will return 10859 days from toDaysPart() but only 50 seconds from toSecondsPart() so you'd miss 22 hours and 54 minutes in between. Using Temporal.until() as in my original answer gives you the ability to express the example duration as "10859 days and 82490 seconds"

Manual calculations

A final option that _may_ be faster than the others(albeit probably very negligible) would be to calculate everything yourself.

The basic idea is to get the milliseconds between the 2 dates and then do your calculations. For sake of simplicity I'll use Duration.toMillis() but there are other options as well which might be faster.

 long millis = Duration.between(fromDateTime, toDateTime).toMillis(); long millisPerDay = 24 * 60 * 60 * 1000; //24 hours * 60 minutes * 60 seconds * 1000 millis long days = millis / millisPerDay; //example yields 10859 long seconds = millis / 1000; //example yields 938300090 

Notes:

  • This may be the fastest calculation and be sufficient for simple use cases
  • If you're using larger units like months or years you may need to take length differences into account (months range from 28 to 31 days, years from 365 to 366 days, etc.) which would make this more complex and especially error prone
added 3711 characters in body
Source Link
Thomas
  • 88.9k
  • 13
  • 126
  • 162

Unfortunately, there doesn'tThere seems to be some confusion on how to calculate the difference between 2 points in time in Java and how to apply the answers to the OP's question.

Since many people seem to be directed here I'll update my answer to provide a period class that spans timemore comprehensive view of what others suggested as well, - so you might havekudos to do the calculations on your ownthem.

Fortunately,Since Java 8 introduced the date and time classes have a lot of utility methods that simplify that to some degreejava.time API this answer will only deal with this API. Here's a wayIf you're using Java 7 or earlier you could use Joda Time to calculatedo something similar.

Before we start with the difference although not necessarilyvarious approaches, let's establish our test data as per the fastestOP's request:

LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55); LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 10, 6, 40, 45);   

Difference between 2 timestamps in a single unit

If you want to get the difference in a single unit, let's say days or seconds you can directly use several options provided by java.time:

Duration

Duration d = Duration.between(fromDateTime, toDateTime); long days = d.toDays(); long seconds = d.toSeconds();

Notes:

  • Getting units larger than days is not straight forward.
  • The toXxx() methods won't return fractional units so the example above would return 10859 days instead of about 10859.95 days. So if you need fractional units you need to calculate yourself, e.g. double days = d.toMillis() / (24 * 60 * 60 * 1000.0); (or double days = d.toMillis() / (double)Duration.ofDays(1).toMillis();)

ChronoUnit

long days = ChronoUnit.DAYS.between(fromDateTime, toDateTime); long seconds = ChronoUnit.SECONDS.between(fromDateTime, toDateTime);

Notes:

  • This may be easier to read than Duration.between(...).toDays()
  • Units up to centuries are supported.
  • As with Duration.toXxx() this doesn't support fractional units so you'd need to calculate yourself.
  • If you need to express the same period in different units using Duration may be faster

Temporal.until()

long days = fromDateTime.until(toDateTime, ChronoUnit.DAYS);long days = fromDateTime.until(toDateTime, ChronoUnit.SECONDS);

Notes:

  • ChronoUnit.between() actually uses this behind the scenes so the same limitations apply. It's more a matter of style and readbility.
  • Temporal is implemented by several classes, e.g. LocalDateTime, LocalTime, ZonedDateTime etc.

Difference between 2 timestamps in a multiple parts with different units

Semi-manual calculations

This is my original answer which deals with the question on how to split a duration into multiple fractional units, e.g. 29 years + 8 months etc.
LocalDateTime tempDateTime = LocalDateTime.from( fromDateTime ); long years = tempDateTime.until( toDateTime, ChronoUnit.YEARS ); tempDateTime = tempDateTime.plusYears( years ); long months = tempDateTime.until( toDateTime, ChronoUnit.MONTHS ); tempDateTime = tempDateTime.plusMonths( months ); long days = tempDateTime.until( toDateTime, ChronoUnit.DAYS ); tempDateTime = tempDateTime.plusDays( days ); long hours = tempDateTime.until( toDateTime, ChronoUnit.HOURS ); tempDateTime = tempDateTime.plusHours( hours ); long minutes = tempDateTime.until( toDateTime, ChronoUnit.MINUTES ); tempDateTime = tempDateTime.plusMinutes( minutes ); long seconds = tempDateTime.until( toDateTime, ChronoUnit.SECONDS ); System.out.println( years + " years " + months + " months " + days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds."); //prints: 29 years 8 months 24 days 22 hours 54 minutes 50 seconds. 

Finally a disclaimer: I didn't take different timezones into account (both dates should be in the same timezone) and I also didn't test/check how daylight saving time or other changes in a calendar (like the timezone changes in Samoa) affect this calculation. So use with care.

Notes:

  • This lets you calculate for all the units you need.
  • Make sure you get the calculations into the right order, i.e. from largest unit to smallest

Duration.toDaysPart() etc.

As of Java 9 (which got published 3 years after the OP's question) the Duration class now has a couple of toXxxPart() methods so you could do the following:

Duration d = Duration.between(fromDateTime, toDateTime); long days = d.toDaysPart(); long seconds = d.toSecondsPart();

Notes:

  • Units above days are not supported as of Java 21
  • Units in between will be missed, i.e. the example above will return 10859 days from toDaysPart() but only 50 seconds from toSecondsPart() so you'd miss 22 hours and 54 minutes in between. Using Temporal.until() as in my original answer gives you the ability to express the example duration as "10859 days and 82490 seconds"

Unfortunately, there doesn't seem to be a period class that spans time as well, so you might have to do the calculations on your own.

Fortunately, the date and time classes have a lot of utility methods that simplify that to some degree. Here's a way to calculate the difference although not necessarily the fastest:

LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55); LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 10, 6, 40, 45);   LocalDateTime tempDateTime = LocalDateTime.from( fromDateTime ); long years = tempDateTime.until( toDateTime, ChronoUnit.YEARS ); tempDateTime = tempDateTime.plusYears( years ); long months = tempDateTime.until( toDateTime, ChronoUnit.MONTHS ); tempDateTime = tempDateTime.plusMonths( months ); long days = tempDateTime.until( toDateTime, ChronoUnit.DAYS ); tempDateTime = tempDateTime.plusDays( days ); long hours = tempDateTime.until( toDateTime, ChronoUnit.HOURS ); tempDateTime = tempDateTime.plusHours( hours ); long minutes = tempDateTime.until( toDateTime, ChronoUnit.MINUTES ); tempDateTime = tempDateTime.plusMinutes( minutes ); long seconds = tempDateTime.until( toDateTime, ChronoUnit.SECONDS ); System.out.println( years + " years " + months + " months " + days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds."); //prints: 29 years 8 months 24 days 22 hours 54 minutes 50 seconds. 

Finally a disclaimer: I didn't take different timezones into account (both dates should be in the same timezone) and I also didn't test/check how daylight saving time or other changes in a calendar (like the timezone changes in Samoa) affect this calculation. So use with care.

There seems to be some confusion on how to calculate the difference between 2 points in time in Java and how to apply the answers to the OP's question.

Since many people seem to be directed here I'll update my answer to provide a more comprehensive view of what others suggested as well - so kudos to them.

Since Java 8 introduced the java.time API this answer will only deal with this API. If you're using Java 7 or earlier you could use Joda Time to do something similar.

Before we start with the various approaches, let's establish our test data as per the OP's request:

LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55); LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 10, 6, 40, 45); 

Difference between 2 timestamps in a single unit

If you want to get the difference in a single unit, let's say days or seconds you can directly use several options provided by java.time:

Duration

Duration d = Duration.between(fromDateTime, toDateTime); long days = d.toDays(); long seconds = d.toSeconds();

Notes:

  • Getting units larger than days is not straight forward.
  • The toXxx() methods won't return fractional units so the example above would return 10859 days instead of about 10859.95 days. So if you need fractional units you need to calculate yourself, e.g. double days = d.toMillis() / (24 * 60 * 60 * 1000.0); (or double days = d.toMillis() / (double)Duration.ofDays(1).toMillis();)

ChronoUnit

long days = ChronoUnit.DAYS.between(fromDateTime, toDateTime); long seconds = ChronoUnit.SECONDS.between(fromDateTime, toDateTime);

Notes:

  • This may be easier to read than Duration.between(...).toDays()
  • Units up to centuries are supported.
  • As with Duration.toXxx() this doesn't support fractional units so you'd need to calculate yourself.
  • If you need to express the same period in different units using Duration may be faster

Temporal.until()

long days = fromDateTime.until(toDateTime, ChronoUnit.DAYS);long days = fromDateTime.until(toDateTime, ChronoUnit.SECONDS);

Notes:

  • ChronoUnit.between() actually uses this behind the scenes so the same limitations apply. It's more a matter of style and readbility.
  • Temporal is implemented by several classes, e.g. LocalDateTime, LocalTime, ZonedDateTime etc.

Difference between 2 timestamps in a multiple parts with different units

Semi-manual calculations

This is my original answer which deals with the question on how to split a duration into multiple fractional units, e.g. 29 years + 8 months etc.
LocalDateTime tempDateTime = LocalDateTime.from( fromDateTime ); long years = tempDateTime.until( toDateTime, ChronoUnit.YEARS ); tempDateTime = tempDateTime.plusYears( years ); long months = tempDateTime.until( toDateTime, ChronoUnit.MONTHS ); tempDateTime = tempDateTime.plusMonths( months ); long days = tempDateTime.until( toDateTime, ChronoUnit.DAYS ); tempDateTime = tempDateTime.plusDays( days ); long hours = tempDateTime.until( toDateTime, ChronoUnit.HOURS ); tempDateTime = tempDateTime.plusHours( hours ); long minutes = tempDateTime.until( toDateTime, ChronoUnit.MINUTES ); tempDateTime = tempDateTime.plusMinutes( minutes ); long seconds = tempDateTime.until( toDateTime, ChronoUnit.SECONDS ); System.out.println( years + " years " + months + " months " + days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds."); //prints: 29 years 8 months 24 days 22 hours 54 minutes 50 seconds. 

Finally a disclaimer: I didn't take different timezones into account (both dates should be in the same timezone) and I also didn't test/check how daylight saving time or other changes in a calendar (like the timezone changes in Samoa) affect this calculation. So use with care.

Notes:

  • This lets you calculate for all the units you need.
  • Make sure you get the calculations into the right order, i.e. from largest unit to smallest

Duration.toDaysPart() etc.

As of Java 9 (which got published 3 years after the OP's question) the Duration class now has a couple of toXxxPart() methods so you could do the following:

Duration d = Duration.between(fromDateTime, toDateTime); long days = d.toDaysPart(); long seconds = d.toSecondsPart();

Notes:

  • Units above days are not supported as of Java 21
  • Units in between will be missed, i.e. the example above will return 10859 days from toDaysPart() but only 50 seconds from toSecondsPart() so you'd miss 22 hours and 54 minutes in between. Using Temporal.until() as in my original answer gives you the ability to express the example duration as "10859 days and 82490 seconds"
syntax highlight
Source Link
LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55); LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 10, 6, 40, 45); LocalDateTime tempDateTime = LocalDateTime.from( fromDateTime ); long years = tempDateTime.until( toDateTime, ChronoUnit.YEARS ); tempDateTime = tempDateTime.plusYears( years ); long months = tempDateTime.until( toDateTime, ChronoUnit.MONTHS ); tempDateTime = tempDateTime.plusMonths( months ); long days = tempDateTime.until( toDateTime, ChronoUnit.DAYS ); tempDateTime = tempDateTime.plusDays( days ); long hours = tempDateTime.until( toDateTime, ChronoUnit.HOURS ); tempDateTime = tempDateTime.plusHours( hours ); long minutes = tempDateTime.until( toDateTime, ChronoUnit.MINUTES ); tempDateTime = tempDateTime.plusMinutes( minutes ); long seconds = tempDateTime.until( toDateTime, ChronoUnit.SECONDS ); System.out.println( years + " years " + months + " months " + days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds."); //prints: 29 years 8 months 24 days 22 hours 54 minutes 50 seconds. 
LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55); LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 10, 6, 40, 45); LocalDateTime tempDateTime = LocalDateTime.from( fromDateTime ); long years = tempDateTime.until( toDateTime, ChronoUnit.YEARS ); tempDateTime = tempDateTime.plusYears( years ); long months = tempDateTime.until( toDateTime, ChronoUnit.MONTHS ); tempDateTime = tempDateTime.plusMonths( months ); long days = tempDateTime.until( toDateTime, ChronoUnit.DAYS ); tempDateTime = tempDateTime.plusDays( days ); long hours = tempDateTime.until( toDateTime, ChronoUnit.HOURS ); tempDateTime = tempDateTime.plusHours( hours ); long minutes = tempDateTime.until( toDateTime, ChronoUnit.MINUTES ); tempDateTime = tempDateTime.plusMinutes( minutes ); long seconds = tempDateTime.until( toDateTime, ChronoUnit.SECONDS ); System.out.println( years + " years " + months + " months " + days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds."); //prints: 29 years 8 months 24 days 22 hours 54 minutes 50 seconds. 
LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55); LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 10, 6, 40, 45); LocalDateTime tempDateTime = LocalDateTime.from( fromDateTime ); long years = tempDateTime.until( toDateTime, ChronoUnit.YEARS ); tempDateTime = tempDateTime.plusYears( years ); long months = tempDateTime.until( toDateTime, ChronoUnit.MONTHS ); tempDateTime = tempDateTime.plusMonths( months ); long days = tempDateTime.until( toDateTime, ChronoUnit.DAYS ); tempDateTime = tempDateTime.plusDays( days ); long hours = tempDateTime.until( toDateTime, ChronoUnit.HOURS ); tempDateTime = tempDateTime.plusHours( hours ); long minutes = tempDateTime.until( toDateTime, ChronoUnit.MINUTES ); tempDateTime = tempDateTime.plusMinutes( minutes ); long seconds = tempDateTime.until( toDateTime, ChronoUnit.SECONDS ); System.out.println( years + " years " + months + " months " + days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds."); //prints: 29 years 8 months 24 days 22 hours 54 minutes 50 seconds. 
LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55); LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 10, 6, 40, 45); LocalDateTime tempDateTime = LocalDateTime.from( fromDateTime ); long years = tempDateTime.until( toDateTime, ChronoUnit.YEARS ); tempDateTime = tempDateTime.plusYears( years ); long months = tempDateTime.until( toDateTime, ChronoUnit.MONTHS ); tempDateTime = tempDateTime.plusMonths( months ); long days = tempDateTime.until( toDateTime, ChronoUnit.DAYS ); tempDateTime = tempDateTime.plusDays( days ); long hours = tempDateTime.until( toDateTime, ChronoUnit.HOURS ); tempDateTime = tempDateTime.plusHours( hours ); long minutes = tempDateTime.until( toDateTime, ChronoUnit.MINUTES ); tempDateTime = tempDateTime.plusMinutes( minutes ); long seconds = tempDateTime.until( toDateTime, ChronoUnit.SECONDS ); System.out.println( years + " years " + months + " months " + days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds."); //prints: 29 years 8 months 24 days 22 hours 54 minutes 50 seconds. 
added 276 characters in body
Source Link
Thomas
  • 88.9k
  • 13
  • 126
  • 162
Loading
Source Link
Thomas
  • 88.9k
  • 13
  • 126
  • 162
Loading