What is the best way to retrieve the dates for last Monday and Friday
Asked Answered
E

5

15

I need to get the dates for Monday and Friday last week. To do this, i am getting the date of Monday this week and subtracting 7 days. This gives me the date for Monday last week.

To get the date for Friday i have to add 4. This confused me a bit because for some reason the first day of the week is Sunday as opposed to Monday here in the UK.

Anyway, here is how i am getting the dates.

            // Get the dates for last MON & FRI
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.add(Calendar.DAY_OF_WEEK, -7);

        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.MILLISECOND,0);

        // Get the date on Friday
        cal.add(Calendar.DAY_OF_WEEK, 4);

        cal.set(Calendar.HOUR_OF_DAY,23);
        cal.set(Calendar.MINUTE,59);
        cal.set(Calendar.SECOND,59);
        cal.set(Calendar.MILLISECOND,0);

The above works but i am interested if there is anything wrong with the logic. I.e. will it work for Februarys, leap years etc.

Feel free to suggest a better solution/approach.

Thanks

Espino answered 30/9, 2012 at 12:19 Comment(4)
Since weeks are regular I don't think there is any problem with february or leap years: if today is monday the last monday was seven days ago.Pileous
If you ran it yesterday, what result would you expect? What if you ran it today? Yesterday it'd give 2012-09-17 as monday, today 2012-09-24.Creatural
I want it to return the date for Mon and Fri last week. So if i run it yesterday (or even today) it should give me 17th and 21st for both runs. If i run it tomorrow which is a new week it should give me 24th and 29th.Espino
How do you define “last week”? Last seven days? Standard Monday-Sunday period? Localized week, such as Sunday-Saturday in the United States? The week prior to today’s week or including today’s partial week?Tarnopol
T
36

tl;dr

get the dates for Monday and Friday last week

LocalDate                                           // Represent a date only, without a time-of-day, and without a time zone or offset.
.now                                                // Capture the current date as seen through the wall-clock time used by the people of a certain region (a time zone). 
( 
    ZoneId.of( "America/Montreal" ) 
)                                                   // Returns a `LocalDate` object.
.with                                               // Move to another date.
( 
    TemporalAdjusters.previous( DayOfWeek.MONDAY )  // Returns an implementation of the `TemporalAdjuster` interface.
)                                                   // Returns another `LocalDate` object, separate and distinct from our original `LocalDate` object. Per the immutable objects design pattern. 

Avoid legacy date-time classes

The other Answers use the troublesome old legacy date-time classes now supplanted by the java.time questions.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

ZoneId z = ZoneId.of( “America/Montreal” );
LocalDate today = LocalDate.now( z );

TemporalAdjuster

The TemporalAdjuster interface provides for adjustments to move from one date-time value to another. Find handy implementations in the TemporalAdjusters class (note the plural 's'). The previous adjuster finds any specified object from the DayOfWeek enum.

The Question does not exactly define “last week”. Last seven days? Standard Monday-Sunday period? Localized week, such as Sunday-Saturday in the United States? The week prior to today’s week or including today’s partial week?

I will assume the prior seven days were intended.

LocalDate previousMonday = today.with( TemporalAdjusters.previous( DayOfWeek.MONDAY ) ) ;
LocalDate previousFriday = today.with( TemporalAdjusters.previous( DayOfWeek.FRIDAY ) ) ;

By the way, if you want to consider the initial date if it happens to already be the desired day-of-week, use alternate TemporalAdjuster implementations: previousOrSame or nextOrSame.


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.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

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.

Tarnopol answered 19/9, 2016 at 19:47 Comment(3)
the 'previousMonday' value might be this week's MondayHighflier
@qianlei No, not this week’s Monday. The Question explicitly asked for “the date for Monday last week”. But if one were wanting to use the initial date if it already is on the desired day-of-week, use an alternate TemporalAdjuster implementation: TemporalAdjusters.previousOrSame( DayOfWeek ) or TemporalAdjusters.nextOrSame( DayOfWeek ).Tarnopol
jshell> LocalDate.now(ZoneId.of("Asia/Kolkata")) $3 ==> 2022-07-21 jshell> LocalDate.now(ZoneId.of("Asia/Kolkata")).getDayOfWeek() $4 ==> THURSDAY jshell> LocalDate.now(ZoneId.of("Asia/Kolkata")).with(TemporalAdjusters.previous(DayOfWeek.MONDAY)); $5 ==> 2022-07-18 jshell> LocalDate.now(ZoneId.of("Asia/Kolkata")).with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)) $8 ==> 2022-07-18Thereinto
M
9

Note: For Java 8 and above please take a look at Basil Bourque's answer (link).
Java 8 introduced a new time/date API which offers most of Joda-Time's functionality.


Joda-Time offers really nice methods for problems like that.

Getting the dates for Monday and Friday last week would look something like this using Joda Time:

DateTime today = DateTime.now();
DateTime sameDayLastWeek = today.minusWeeks(1);
DateTime mondayLastWeek = sameDayLastWeek.withDayOfWeek(DateTimeConstants.MONDAY);
DateTime fridayLastWeek = sameDayLastWeek.withDayOfWeek(DateTimeConstants.FRIDAY);

You can create DateTime objects from java.util.Date objects and vice versa so it is easy to use with Java dates.


Using the above code with the date

DateTime today = new DateTime("2012-09-30");

results in "2012-09-17" for Monday and "2012-09-21" for Friday, setting the date to

DateTime tomorrow = new DateTime("2012-10-01");

results in "2012-09-24" for Monday and "2012-09-28" for Friday.

Mitchell answered 30/9, 2012 at 12:44 Comment(0)
C
6

You still have start of week set to sunday, which means that Calendar.MONDAY on a saturday is the monday before, while Calendar.MONDAY on a sunday is the next day.

What you need to do is (according to how you want it according to your comment above), to set the start of week to monday.

Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);

cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.add(Calendar.DAY_OF_WEEK, -7); 
...

Beyond that, and that the last second of friday isn't included in the range, your logic seems sound, and shouldn't have trouble with leap years/DST shifts etc.

Creatural answered 30/9, 2012 at 13:16 Comment(1)
I would shortly add that if the current day of the week is Monday, you would still go to the previous Monday in case one is wondering about this edge case.Zelmazelten
M
1

The only thing I see wrong is that you are in fact testing the range Mo-Fr, and not, as stated, retrieving two specific days. It would be safer to test range Mo-Sa with exclusive upper bound.

Mu answered 30/9, 2012 at 12:27 Comment(5)
I dont quite understand what you mean. Retreiving two specific dates (Monday and Friday) is what i am trying to do. Do you mean that i should get the range for Monday to Saturday then get the dates for Monday and Friday from that week?Espino
Ziggy, I see that you set the time to 23:59:59 for the Friday date. This is a strong indication to me that your intention is to range-check some dates against this as an upper bound. My suggestion is to use Saturday midnight instead as the exclusive upper bound.Mu
@gekkostate As requested, I suggested a better approach. Why do you think that doesn't qualify as an answer? Does the word count alone bother you?Mu
Hi Marko, i need to run a query in a database to retrieve data for a report. The data i want to include in the report is only from Monday morning to Friday evening. I don't want to include data for Saturday.Espino
Ziggy that's exactly my point. Using Saturday midnight as the exclusive upper bound is the correct way to achieve your goal.Mu
S
1

You can use TemporalAdjusters to adjust the desired dates/days you are looking for.

Example:

LocalDate today = LocalDate.now();
LocalDate lastMonday = today.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
LocalDate lastFriday = today.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));
Sower answered 9/2, 2023 at 6:26 Comment(1)
I covered this in my Answer.Tarnopol

© 2022 - 2024 — McMap. All rights reserved.