2

From my UI I'm passing two LocalTime values; from time and to time. Both times are

LocalTime HH:mm

formatted. Now I need to check whether these times are in the same day. For an example if someone passes fromTime - 18:00 and toTime - 10:00, I need throw an exception. I need to check whether both times are in same day.

I tried with DateUtils.isSameDay() but it accepts dates. In my case I have only time.

Appreciate your help.

Thanks

neo mal
  • 67
  • 1
  • 8
  • 4
    "if someone passes fromTime - 18:00 and toTime - 10:00, I need throw an exception" <- so in other words you want to check if one time is after the other and not before? There are methods just for that (`isAfter` and `isBefore`). Times don't have any day so "check if both times in the same day" makes no sense. – OH GOD SPIDERS Feb 24 '21 at 10:35
  • 2
    `if (fromTime.isBefore(toTime))` – Sweeper Feb 24 '21 at 10:37

1 Answers1

3

LocalTime does not store or represent a date or time-zone. So you can't check if two instances are on the same date.

Just see if the "from" time is before the "to" time.

If you need to store date as well, use LocalDateTime.

M A
  • 71,713
  • 13
  • 134
  • 174