Get date of first day of week based on LocalDate.now() in Java 8

前端 未结 9 1521
梦谈多话
梦谈多话 2020-12-02 19:35

I would like the get the date of the first day of the week based on LocalDate.now(). The following was possible with JodaTime, but seems to be removed from the new Date API

9条回答
  •  长情又很酷
    2020-12-02 20:31

    public void getWeekFromADateOfAMonth(){
        String date = "2019-01-02T18:25:43.511Z";
        TemporalField fieldISO = WeekFields.of(Locale.US).dayOfWeek();
        ZonedDateTime dateTime = ZonedDateTime.parse(date);
    
        int week = dateTime.get ( IsoFields.WEEK_OF_WEEK_BASED_YEAR );
        int weekYear = dateTime.get ( IsoFields.WEEK_BASED_YEAR );
    
        System.out.println ( "now: " + dateTime + " is week: " + week + " of weekYear: " + weekYear );
    
        int startDate = dateTime.with(fieldISO,1).getDayOfMonth();
        int endDate = dateTime.with(fieldISO,7).getDayOfMonth();
    
        String startMonth = String.valueOf(dateTime.with(fieldISO,1).getMonth());
        String endMonth = String.valueOf(dateTime.with(fieldISO,7).getMonth());
    }
    

提交回复
热议问题