Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 336
Ian Avatar asked Jul 01 '11 09:07

Ian


2 Answers

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. e.g., 0 for January. dayOfMonth the value used to set the DAY_OF_MONTH calendar field in the calendar.

like image 101
Petar Ivanov Avatar answered Oct 05 '22 22:10

Petar Ivanov


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?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • See How to use ThreeTenABP….

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.

like image 41
Basil Bourque Avatar answered Oct 06 '22 00:10

Basil Bourque