How to get the last Sunday before current date?

后端 未结 7 1189
花落未央
花落未央 2021-02-12 15:07

I have the following code for getting the last Sunday before the current date:

Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.WEEK_OF_YEAR, cale         


        
7条回答
  •  清歌不尽
    2021-02-12 16:05

    The following works for me irrespective of which month and year.

     Calendar cal = Calendar.getInstance(TimeZone.getDefault());
     Date date = cal.getTime();
     int days = cal.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY;
     date.setTime(date.getTime() - (long) (days*1000*60*60*24));
     cal.setTime(date);
    

提交回复
热议问题