Java Date & Time of Day

I have an application that runs in java.util.Date. I want to check if this date is at a specified time of the day (for example, between 10:30 and 11:30), I do not need a date, just the time of day.

Can someone show me an easy way to do this?

thank

+3
source share
1 answer

This is a class Calendar. Assuming what dateis your dateobject:

Calendar cal = Calendar.getInstance();
cal.setTime(date);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minutes = cal.get(Calendar.MINUTE);
if (hour == 10 && minutes >= 30 || hour == 11 && minutes <= 30) {
   ... 
}
+13
source

Source: https://habr.com/ru/post/1736635/


All Articles