0

Java Q: On any given day, I want to determine the date on which (say) last Friday fell. Example: If I run my program today (ie. Wednesday, 05th Sep 12), I should get the result as "Last Friday was on 31st Aug 12". If I run it on Saturday, 08th Sep 12, the result should be 07th Sep 12, and so on (The date formatting is not strictly an issue here though)

Is there any available api, or do I need to write a program at length going back that many days based on the current day, etc?

Thank you!

user1639485
  • 808
  • 3
  • 14
  • 26
  • 1
    Are you familiar with JodaTime? – Thorbjørn Ravn Andersen Sep 05 '12 at 14:32
  • possible duplicate of [How to get the past Sunday and the coming Sunday in Java?](http://stackoverflow.com/questions/4536230/how-to-get-the-past-sunday-and-the-coming-sunday-in-java) and [this](http://stackoverflow.com/q/10328584/642706) and [this](http://stackoverflow.com/q/22890644/642706) and many others. – Basil Bourque Jun 30 '14 at 07:14

3 Answers3

3

How about this:

Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DAY_OF_WEEK);
cal.add(Calendar.DAY_OF_MONTH, -((day + 1) % 7));
Date lastFriday = cal.getTime();

We can always go back to the previous Friday by subtracting the Calendar.DAY_OF_WEEK value for the current date, plus 1. For example, if the current day is Monday (value=2) and we subtract (2 + 1) we go back 3 days to Friday. If we do the same thing on a Tuesday we go back (3 + 1) days - also to a Friday.

If the current day is either Friday or Saturday we need to be sure that we only go back 0 or 1 day respectively, so we just take mod 7 of the (day + 1) value.

codebox
  • 19,927
  • 9
  • 63
  • 81
  • Where does this actually do any logic related to Friday? – djechlin Sep 05 '12 at 14:40
  • Awesome, +1! Just a little side note: `cal.add();` returns `void`. – sp00m Sep 05 '12 at 14:58
  • The code works just fine, except that on a Friday, it wouldn't show me the **Last** Friday date, but would show the current date. Any fix for that? – user1639485 Sep 05 '12 at 15:12
  • A little tweak and I am fine: int day = cal.get(Calendar.DAY_OF_WEEK); int dayDiff = (day+1)%7; if(dayDiff == 0) dayDiff = 7; cal.add(Calendar.DAY_OF_MONTH, - dayDiff); – user1639485 Sep 05 '12 at 15:17
0
int day = cal.get(Calendar.DAY_OF_WEEK);  
int dayDiff = (day+1)%7;  
if(dayDiff == 0)  
  dayDiff = 7;  
cal.add(Calendar.DAY_OF_MONTH, - dayDiff);
user1639485
  • 808
  • 3
  • 14
  • 26
0

I recently developed Lamma Date which is particularly designed for this use case:

new Date(2014, 7, 1).previous(DayOfWeek.FRIDAY); // 2014-06-27
new Date(2014, 7, 2).previous(DayOfWeek.FRIDAY); // 2014-06-27
new Date(2014, 7, 3).previous(DayOfWeek.FRIDAY); // 2014-06-27
new Date(2014, 7, 4).previous(DayOfWeek.FRIDAY); // 2014-06-27
new Date(2014, 7, 5).previous(DayOfWeek.FRIDAY); // 2014-07-04
new Date(2014, 7, 6).previous(DayOfWeek.FRIDAY); // 2014-07-04
new Date(2014, 7, 7).previous(DayOfWeek.FRIDAY); // 2014-07-04
Max
  • 2,065
  • 24
  • 20