What is the best way to get dates for last Monday and Friday

I need to get dates on Monday and Friday last week. To do this, I get the Monday date for this week and subtract 7 days. This gives me a date on Monday last week.

To get the date on Friday, I have to add 4. This confused me a bit, because for some reason the first day of the week is Sunday, not Monday in the UK.

Anyway, this is how I get 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'm interested in something wrong with the logic. That is, will it work in February, leap years, etc.

Feel free to suggest a better solution / approach.

thank

+5
source share
4 answers

.. Java 8 ().
Java 8 API /, Joda-Time.


Joda-Time .

, Joda Time:

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

DateTime java.util.Date , Java.


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

"2012-09-17" "2012-09-21" ,

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

"2012-09-24" "2012-09-28" .

+6

TL;DR

LocalDate previousMonday = 
    LocalDate.now( ZoneId.of( "America/Montreal" ) )
             .with( TemporalAdjusters.previous( DayOfWeek.MONDAY ) ) ;

, java.time.

LocalDate

LocalDate .

. . , - , "" .

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

TemporalAdjuster

TemporalAdjuster . TemporalAdjusters ( ). previous DayOfWeek enum.

" ". ? - ? , - ? ?

, .

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

, , ​​, TemporalAdjuster: previousOrSame nextOrSame.


java.time

java.time Java 8 . legacy , java.util.Date, Calendar SimpleDateFormat.

Joda-Time, , java.time.

, . Oracle. Qaru . JSR 310.

java.time . JDBC, JDBC 4.2 , , java.sql.*.

java.time?

ThreeTen-Extra java.time . java.time. , Interval, YearWeek, YearQuarter .

+5

, , , Calendar.MONDAY - , Calendar.MONDAY - .

( , , ), .

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

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

In addition, and that the last second of Friday is not included in the range, your logic seems to be sound and should not have problems with years / DST shift changes, etc.

+2
source

The only thing I see wrong is that you are actually checking the Mo-Fr range , and not, as indicated, extracting two specific days. It would be safer to test the Mo-Sa range with an exceptional upper limit.

+1
source

All Articles