How to check if time in day is between two times?

前端 未结 5 745
说谎
说谎 2021-01-27 16:24

In my app I create an object that represents a high school class. This object holds 2 Calendar objects that represents the class\'s start and stop time each day. When a user c

5条回答
  •  广开言路
    2021-01-27 16:49

    JDK 8 Date-Time APIs are a good approach to solving these kinds of issues. Instead of Calendar , use LocalTime to store the start and end time of the class.

    LocalTime now = LocalTime.now(ZoneId.systemDefault());
    LocalTime start = mList.get(a).getStartTime();
    LocalTime end = mList.get(a).getEndTime();
    if(now.isAfter(start) && now.isBefore(end)){
        //do something
    }
    

    For Android, you can use The ThreeTenABP project which adapts the java.time APIs for Android.

提交回复
热议问题