java.time equivalent of Joda-Time `withTimeAtStartOfDay`? (get first moment of the day)

后端 未结 4 1717
野性不改
野性不改 2021-02-19 07:02

In the Joda-Time library, the DateTime class offers a method withTimeAtStartOfDay to get the first moment of the day. You might think of that moment as \"midnight\". That first

4条回答
  •  感动是毒
    2021-02-19 07:25

    Truncate

    While this is an old question, I've just discovered another way to do this, using truncation, which to me feels more elegant:

    ZonedDateTime now = ZonedDateTime.now();
    ZonedDateTime today = now.truncateTo(ChronoUnit.DAYS); // today, midnight
    

    This is shorter to type and requires fewer intermediate values in code. As far as I can tell, the semantics are the same as the accepted answer's.

提交回复
热议问题