Get last week date range for a date in Java

后端 未结 4 1227
一个人的身影
一个人的身影 2020-12-11 01:52

Suppose I have a Date 20 June 2013

How can I get the Date range for the last week, ie in this case 9 June to 15 June.

Also if the date was 2nd June 2013

4条回答
  •  既然无缘
    2020-12-11 02:29

    You can use JodaTime for a cleaner solution. With JodaTime you can do as below:

    final DateTime input = new DateTime();
    System.out.println(input);
    final DateMidnight startOfLastWeek = 
               new DateMidnight(input.minusWeeks(1).withDayOfWeek(DateTimeConstants.MONDAY));
    System.out.println(startOfLastWeek);
    final DateMidnight endOfLastWeek = startOfLastWeek.plusDays(6);
    System.out.println(endOfLastWeek);
    

提交回复
热议问题