Skip to content
Advertisement

How to check if two LocalTimes are in the same day?

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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement