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

前端 未结 9 1503
梦谈多话
梦谈多话 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:25

    Thanks to come from in this question.

    Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
            cal.clear(Calendar.MINUTE);
            cal.clear(Calendar.SECOND);
            cal.clear(Calendar.MILLISECOND);
    
            // get start of this week in milliseconds
            cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
            System.out.println("Start of this week:       " + cal.getTime());
            System.out.println("... in milliseconds:      " + cal.getTimeInMillis());
    

提交回复
热议问题