0

I could not find any hand method on java.util.Date function in order to get number of days between 2 dates?

How should I get number of days?

cchantep
  • 9,118
  • 3
  • 30
  • 41
Alireza
  • 6,497
  • 13
  • 59
  • 132
  • 3
    1/ Don't use `java.util.Date`, it's outdated. Use `java.time` classes. For instance `Instant` could be a good replacement for your use case with an easy way to get the duration between two instances. – Gaël J Dec 26 '22 at 08:06
  • 2
    2/ Is the question about the `Option` wrapper or not? You mention it in the title but then it seems you don't have any issue with it? – Gaël J Dec 26 '22 at 08:07
  • 1
    @GaëlJ unfortunately it's a legacy code and API is bound to external clients. So I prefer to use `java.util.Date` – Alireza Dec 26 '22 at 08:07
  • 1
    @GaëlJ I removed Option part so to make the question more clear – Alireza Dec 26 '22 at 08:08
  • 1
    Does this answer your question? [Calculating the difference between two Java date instances](https://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances) – Gaël J Dec 26 '22 at 08:10
  • 2
    A `Date` represents an instant in time. Two instants could be on the same date in one time zone, and on different dates in another time zone. Do you know which time zone you should use to compute the result? – Jon Skeet Dec 26 '22 at 08:15
  • 1
    Under [the question that @GaëlJ links to](https://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances) remember to go for the answers that use java.time, the modern date and time API. There are some. – Ole V.V. Dec 26 '22 at 09:10
  • 1
    Did you remember to search before asking? You forgot to tell us. Similar questions have been asked and answered numerous times. – Ole V.V. Dec 26 '22 at 09:12

1 Answers1

3

First, convert your legacy java.util.Date objects to their modern replacements, java.time.Instant. Call new methods added to the old classes.

Instant start = myJavaUtilDate.toInstant() ;

If by “number of days” you mean “number of 24-hour chunks of time”, without regard for calendar, use Duration.

long days = Duration.between( start , end ).toDays() ;

If you meant calendar days, you need to specify the time zone by which you want to perceive dates.

ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;

Apply that zone to Instant to produce a ZonedDateTime. Same moment, same point on the timeline, different wall-clock time and date.

ZonedDateTime startZdt = start.atZone( z ) ;

Calculate elapsed days using ChronoUnit enum DAYS.

long days = ChronoUnit.between( startZdt , endZdt ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • What is `javaUtilDateStart` ? – Alireza Dec 26 '22 at 08:41
  • 2
    @Alireza - `javaUtilDateStart` is any variable referencing an instance of `java.util.Date`. This answer guides you in the right direction. Do not use the error-prone legacy `java.util` date-time API. – Arvind Kumar Avinash Dec 26 '22 at 09:01
  • 1
    I received `Cannot resolve symbol toInstant()` on my startVariable. If it helps `public class Date extends java.util.Date` – Alireza Dec 26 '22 at 09:25
  • 1
    Which Java version are you using? What is the declared type of your `startVariable`? – Ole V.V. Dec 26 '22 at 09:26
  • @OleV.V. its type is `class Date extends java.util.Date` – Alireza Dec 26 '22 at 09:55
  • @OleV.V. sorry it was because of `Option[startVariable]` I used map and then turned its value to `toInstant` – Alireza Dec 26 '22 at 09:57
  • @OleV.V. toInstant returns `throw new java.lang.UnsupportedOperationException()`. I use `OpenJDK 11` – Alireza Dec 26 '22 at 10:50
  • While `java.util.Date`and subclass `java.sql.Timestamp` implement `toInstant`, this is not the case for two other subclasses `java.sql.Date` and `java.sql.Time`. I would expect each of the latter two to throw the exception you mention. So maybe you have got one of those on runtime? – Ole V.V. Dec 26 '22 at 11:07
  • If your `java.util.Date` are really `java.sql.Date`, cast to the latter type and use its `toLocalDate` method for getting `LocalDate` objects. Then pass two `LocalDate`s to `ChronoUnit.DAYS.between()` to get the difference in dates. – Ole V.V. Dec 26 '22 at 11:20