If I have a specific date of a day, how do I get the date of that day in the previous week?

后端 未结 4 1739
长发绾君心
长发绾君心 2021-02-19 18:15

SEE ANSWER FROM @Basil Bourque for most up to date answer


For example, if I have a \"Date\" variable \"date1\" with a value of (dd/mm/yyy) 03/07/2011, which is a S

4条回答
  •  盖世英雄少女心
    2021-02-19 18:53

    If you are willing to use Joda time it will be very easy.

    An example:

    DateTime toDay=new DateTime();
    DateTime dateOfPreviousWeek=toDay.minusDays(7);
    

    Another:

    DateTime toDay = new DateTime(2011, 7, 1, 0, 0, 0, 0);
    DateTime dateOfPreviousWeek = toDay.minusDays(7);
    

    You can get java.util.Date from DateTime as:

    Date javaDate=jodaDateTime.toDate();
    

提交回复
热议问题