How to get the last Sunday before current date?

后端 未结 7 1217
花落未央
花落未央 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 15:41

    Here is a snippet to calculate any last day of the week with Joda:

    import org.joda.time.DateTime
    import org.joda.time.DateTimeConstants
    
    DateTime now = DateTime();
    int offset = ((now.dayOfWeek - DateTimeConstants.THURSDAY) + 7) % 7;
    DateTime lastThursday = now.minusDays(offset);
    

    Just replace DateTimeConstants.THURSDAY with your day of choice.

提交回复
热议问题