简体   繁体   中英

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

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 Sunday. How do I get the "Date" of the previous Sunday "date2"? which would have the value (dd/mm/yyyy) 26/06/2011.

Is there an easy way of doing it, for example:

pseudo code:

Date date1 = (03/07/2011);
Date date2 = date1.subtractNumberOfDays(7);

You should use Calendar:

    Calendar date = new GregorianCalendar(2011, Calendar.JULY, 3);
    date.add(Calendar.DAY_OF_MONTH, -7);
    System.out.println(date.getTime());

You can create a calendar from date too:

    Date date1 = new Date(111, Calendar.JULY, 3);//the year field adds 1900 on to it.
    Calendar date = new GregorianCalendar();
    date.setTime(date1);
    date.add(Calendar.DAY_OF_MONTH, -7);
    date2 = date.getTime();

Be aware that:

  • Java uses 0 to represent January !
  • Date(year, month, day) is deprecated since JDK version 1.1 !

See the GregorianCalendar JavaDoc:

Constructs a GregorianCalendar with the given date set in the default time zone with the default locale. Parameters: year the value used to set the YEAR calendar field in the calendar. month the value used to set the MONTH calendar field in the calendar. Month value is 0-based. eg, 0 for January. dayOfMonth the value used to set the DAY_OF_MONTH calendar field in the calendar.

tl;dr

LocalDate.of( 2011 , Month.JULY , 3 )
         .minusWeeks( 1 ) 

2011-06-26

java.time

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.

Previous week

You can subtract a week from the date.

LocalDate weekPrior = localDate.minusWeeks( 1 );

See this code run live at IdeOne.com .

Previous day-of-week

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 ) ) ;

About java.time

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 .

A more clear approach:

Calendar date = new GregorianCalendar(2011, 1, 1);
date.add(Calendar.WEEK_OF_MONTH, -1);
System.out.println(date.getTime());

use either WEEK_OF_MONTH or WEEK_OF_YEAR

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM