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

前端 未结 5 737
说谎
说谎 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:47

    I will here only provide the Joda-Time-related answer you asked for. Joda-Time has the advantage to offer a dedicated type for the clock time, namely LocalTime. The old java.util.Calendar-stuff does not offer this advantage hence your difficulties.

    First you convert an instance of java.util.Date like follows:

    Date time = ...;
    DateTime dt = new DateTime(time, DateTimeZone.getDefault());
    LocalTime lt = dt.toLocalTime();
    

    Note that the conversion is always timezone dependent. Then you can compare two LocalTime instances using the inherited methods isAfter(...) or isBefore(...).

提交回复
热议问题