How to obtain the start time and end time of a day?

前端 未结 14 917
心在旅途
心在旅途 2020-11-27 10:23

How to obtain the start time and end time of a day?

code like this is not accurate:

 private Date getStartOfDay(Date date) {
    Calendar calendar =          


        
14条回答
  •  误落风尘
    2020-11-27 10:25

    in getEndOfDay, you can add:

    calendar.set(Calendar.MILLISECOND, 999);
    

    Although mathematically speaking, you can't specify the end of a day other than by saying it's "before the beginning of the next day".

    So instead of saying, if(date >= getStartOfDay(today) && date <= getEndOfDay(today)), you should say: if(date >= getStartOfDay(today) && date < getStartOfDay(tomorrow)). That is a much more solid definition (and you don't have to worry about millisecond precision).

提交回复
热议问题