Retrieve current week's Monday's date

前端 未结 8 2123
逝去的感伤
逝去的感伤 2020-12-01 10:59

We have a utility that will run any day between Monday - Friday. It will update some number of files inside a Content Management Tool. The last modified date associated with

8条回答
  •  温柔的废话
    2020-12-01 11:25

    The following will work, including wrapping months:

    Calendar c = Calendar.getInstance();
    c.setFirstDayOfWeek(Calendar.MONDAY);
    c.setTime(new Date());
    int today = c.get(Calendar.DAY_OF_WEEK);
    c.add(Calendar.DAY_OF_WEEK, -today+Calendar.MONDAY);
    System.out.println("Date "+c.getTime());
    

    If, however, you edit your application on a Sunday (eg. Sunday 12 Feb), the date will be for the following Monday. Based on your requirements (the app will only run Monday thru Friday), this should not pose a problem.

提交回复
热议问题