1

I need to print date again after i calculated difference between two dates.

here is what I tried:

 fun getRemainingTime(endTime: ZonedDateTime): Long {
        val currentTime = ZonedDateTime.now(ZoneId.systemDefault())

        return dateUtils.durationDifference(currentTime, endTime).toMillis()
    }

but when I try to convert it to localdate like below again it starts with 1970. So I need to actual date which was calculated. for example: difference between 2022-10-10 and 2022-10-12 should be 2022-10-02

LocalDateTime.ofInstant(Instant.ofEpochMilli(remainingDurationInMillis), ZoneId.systemDefault())
aligur
  • 3,387
  • 3
  • 34
  • 51
  • Sorry, that does not make sense. Say that you have calculated a difference of a minute so got 60000 milliseconds. This number does not imply any *actual date which was calculated*, neither in 1970 nor in 2022. And a `LocalDateTime` is for a date and time of day (not that often useful), not for an amount of time. For your difference one would use `java.time.Duration`. – Ole V.V. May 19 '22 at 05:15

1 Answers1

4

java.time.LocalDateTime was not created to represent a difference between two dates. There are java.time.Period and java.time.Duration that should be used for it (see Oracle docs).

A Duration measures an amount of time using time-based values (seconds, nanoseconds). A Period uses date-based values (years, months, days).

They both have a convenient .between() method, which you could use like this:

Duration diff = Duration.between(ZonedDateTime.now(ZoneId.systemDefault()), endTime);

The reason why there are no combined classes to represent duration as years, months, days AND hours, minutes, seconds is that a day could be 24 or 25 hours depending on Daylight saving time. So

A Duration of one day is exactly 24 hours long. A Period of one day, when added to a ZonedDateTime, may vary according to the time zone. For example, if it occurs on the first or last day of daylight saving time.

I would suggest you to use the Duration class and if you want to pretty print it, you have to do it manually like this (thanks to this answer):

System.out.println(diff.toString()
            .substring(2)
            .replaceAll("(\\d[HMS])(?!$)", "$1 ")
            .toLowerCase());

This will print something like 370h 24m 14.645s. If you want days, months and years, you would have to calculate them from seconds and print.

If you're using Java 9+ there are methods to get number of days, hours, minutes and seconds in the duration:

System.out.println(String.format("%sd %sh %sm %ss", 
            diff.toDaysPart(), 
            diff.toHoursPart(), 
            diff.toMinutesPart(), 
            diff.toSecondsPart()));
Kirill Simonov
  • 8,257
  • 3
  • 18
  • 42
  • 1
    If a combined period with duration is truly needed, you’ll find one in the [*ThreeTen-Extra*](https://www.threeten.org/threeten-extra/) with its [`PeriodDuration`](https://www.threeten.org/threeten-extra/apidocs/org.threeten.extra/org/threeten/extra/PeriodDuration.html). But, as mentioned in Answer, combining those two concepts tends to be problematic in practice. – Basil Bourque May 19 '22 at 00:23