0

In our Java8+ code we have a legacy service returning a java.util.Date object; we want to know if it's in a date-range (with given dates provided as ISO strings), like:

java.util.Date date = legacyService.retrieveLegacyDate(..); // won't change...

if (date.isAfter("2022-08-16") && date.isBefore("2022-10-17")) { // pseudo-code => to FIX
    // date is OK, proceed....
}

How can I "fix" the pseudo-code using only the new types from the java.time API?

Note: for now I couldn't find anything (either here on S.O. or elsewhere) that deals with this kind of problem, because either it's about comparing "Dates" (the legacy object) or LocalDate/LocalDateTime/etc. but always same type objects between them.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
maxxyme
  • 2,164
  • 5
  • 31
  • 48
  • A `java.util.Date` does not represent a year-month-day "date". It represents an "instant" in time. To convert it into a "date", you need an additional timezone. Are you sure you still want to do this? – Sweeper Oct 07 '22 at 08:43
  • 5
    `java.util.Date.toInstant()`? – Mark Rotteveel Oct 07 '22 at 08:44
  • `Date` would normally perceive `2022-08-16` as 2022-08-16 00:00 in hte default time zone. would 2022-08-16 01:00 be considered after that, or do you require it to fall on 2022-08-17 or later? – Ole V.V. Oct 07 '22 at 08:44
  • @Sweeper no I don't want/need timezones. Then should I update my title to something like this: "How to compare a legacy java.util.Date with specific instants in Java 8?" – maxxyme Oct 07 '22 at 08:46
  • 2
    There is no way whatsoever to compare a `Date` to `2022-08-16` wothout considering time zone! – Ole V.V. Oct 07 '22 at 08:47
  • I think here "2022-08-16" should probably be considered at whatever time of the day (so literally as 2022-08-16 23:59) I think... – maxxyme Oct 07 '22 at 08:49
  • 1
    Does this answer your question? [Convert java.util.Date to java.time.LocalDate](https://stackoverflow.com/questions/21242110/convert-java-util-date-to-java-time-localdate) – pringi Oct 07 '22 at 08:51
  • 2
    Well, there is no concept of a "day", if you are talking about instants. "2022-08-16" is not a "specific instant". If you have a date like that, you gotta be considering time zones, because different time zones have different date boundaries. "2022-08-16 23:59" means a different thing in different time zones! – Sweeper Oct 07 '22 at 08:54
  • I'm sorry but I don't understand anything (added to the fact that I'm not a native English speaker). If there are no date/day concepts, if (quoting you) *"2022-08-16" is not a "specific instant"*, then what is "2022-08-16"? Speaking strictly at the business level, we need to check if the "date" (retrieved) is between the other 2 "dates" (excluding). I don't know how to say it otherwise. – maxxyme Oct 07 '22 at 13:46
  • At any point in time the time zones of the world have two or three different dates. This means that conversely 2022-08-16 falls in different periods (usually 24 hours long, not always) in different time zones. So a given point in time represented by a `Date` object could be before 2022-08-16 in one time zone, on 2022-08-16 in another time zone and after 2022-08-16 in yet a different time zone. Yes, this is how surprising date and time handling is. – Ole V.V. Oct 07 '22 at 19:48
  • A concrete example: Say you have a legacy `Date` representing 2022-08-16T10:30:00Z UTC. At this point in time it is 2022-08-15T23:30-11:00[Pacific/Pago_Pago] on Samoa and 2022-08-17T00:30+14:00[Pacific/Kiritimati] on the Kiritimati atoll. Note the day of month is 15 on Samoa and 17 on Kiritimati. Now do you want to consider your legacy `Date` before or after `"2022-08-16"`? – Ole V.V. Oct 08 '22 at 06:11
  • OK but since the `Date` object is just a number of seconds since Epoch, and I don't have to deal with timezones (because this app is local only), how would I proceed? Someone in our team suggested I compare the java.util.Date with other Date objects retrieved from the SDF e.g. `new SimpleDateFormat("yyyy-MM-dd").parse("2022-08-16")` – maxxyme Oct 10 '22 at 08:00

0 Answers0