Get first date of current month in java

前端 未结 9 1797
日久生厌
日久生厌 2020-11-29 05:28

I am trying to get to and from date where ToDate will have previous date and FromDate will have first date of the current month. For January it wou

9条回答
  •  忘掉有多难
    2020-11-29 06:29

    In order to get a Date (that can be used in JPA later on), I did

    Date startOfMonth = Date.from(LocalDate.now().withDayOfMonth(1).atStartOfDay().toInstant(ZoneOffset.UTC));
    
    • I take current date LocalDate.now()
    • Move it to first day of the month withDayOfMonth(1)
    • Move it to the sart of the day atStartOfDay() to get rid of hours and minutes
    • and deal with the TimeZone issues by changing it into an Instant with the "right" ZoneOffset.

提交回复
热议问题