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
LocalDate.of( 2011 , Month.JULY , 3 )
.minusWeeks( 1 )
2011-06-26
The Question and Answers use old outmoded date-time classes. Instead use the java.time classes.
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
LocalDate localDate = LocalDate.of( 2011 , Month.JULY , 3 );
Alternatively, pass an integer in second argument instead of the Month
enum. Pass 1-12 for January-December.
You can subtract a week from the date.
LocalDate weekPrior = localDate.minusWeeks( 1 );
See this code run live at IdeOne.com.
If you want a specific day of the week, use a TemporalAdjuster.
Several such handy implementations provided in the TemporalAdjusters class (note the plural 's').
LocalDate priorTuesday = localDate.with( TemporalAdjusters.previous( DayOfWeek.TUESDAY ) ) ;
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.