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

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

    I had several inconveniences with all the solutions because I needed the type of Instant variable and the Time Zone always interfered changing everything, then combining solutions I saw that this is a good option.

            LocalDate today = LocalDate.now();
            Instant startDate = Instant.parse(today.toString()+"T00:00:00Z");
            Instant endDate = Instant.parse(today.toString()+"T23:59:59Z");
    

    and we have as a result

            startDate = 2020-01-30T00:00:00Z
            endDate = 2020-01-30T23:59:59Z
    

    I hope it helps you

提交回复
热议问题