Determine if a time is between two times regardless of date

前端 未结 3 1917
死守一世寂寞
死守一世寂寞 2021-01-29 10:06

I created a custom TimePicker preference for my Android Wear watch face. The user selects a time and it returns the current time in milliseconds. The code for this

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-29 10:51

    What you can do is create a 2 date objects from this time using this:

    Date first = new Date();
    first.setTime(time_in_milliseconds);
    
    Date second = new Date();
    first.setTime(time_in_milliseconds);
    

    Then you can get current time by this

    Date current = new Date();
    

    Then use this condition:

    if(current.after(first)&¤t.before(second)){
    
    //Do your work
    
    }
    

    Hope this helps.

提交回复
热议问题