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

后端 未结 4 1715
野性不改
野性不改 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:30

    TemporalAdjuster

    Every LocalTime is also a TemporalAdjuster, so if can be passed to the with() method of most temporal types and will update the time fields to match.

    That is:

    @Test
    public void date_time_at_start_of_day() throws Exception {
        assertThat(LocalDateTime.parse("2015-05-22T12:27:00")
                                .with(LocalTime.MIDNIGHT),
                   equalTo(LocalDateTime.parse("2015-05-22T00:00:00")));
        assertThat(OffsetDateTime.parse("2015-05-22T12:27:00+01:00")
                                 .with(LocalTime.MIDNIGHT),
                   equalTo(OffsetDateTime.parse("2015-05-22T00:00:00+01:00")));
        assertThat(ZonedDateTime.parse("2015-05-22T12:27:00+01:00[Europe/London]")
                                .with(LocalTime.MIDNIGHT),
                   equalTo(ZonedDateTime.parse("2015-05-22T00:00:00+01:00[Europe/London]")));
    }
    

提交回复
热议问题