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

前端 未结 14 923
心在旅途
心在旅途 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:49

    I think the easiest would be something like:

    // Joda Time
    
    DateTime dateTime=new DateTime(); 
    
    StartOfDayMillis = dateTime.withMillis(System.currentTimeMillis()).withTimeAtStartOfDay().getMillis();
    EndOfDayMillis = dateTime.withMillis(StartOfDayMillis).plusDays(1).minusSeconds(1).getMillis();
    

    These millis can be then converted into Calendar,Instant or LocalDate as per your requirement with Joda Time.

提交回复
热议问题