Retrieve current week's Monday's date

前端 未结 8 2097
逝去的感伤
逝去的感伤 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:24

    It won't work if the currents week monday is the month before... For example if today is Friday 1st of June... You should probably rather use the roll method...

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题