1

How can I find out the date of last (previous) "Friday" or any other day from a specified date?

public getDateOnDay(Date date, String dayName) {
    // ?
}
Michael Myers
  • 188,989
  • 46
  • 291
  • 292
MohammedYakub M.
  • 2,893
  • 5
  • 31
  • 42
  • 4
    Well, I'd say you're stuck. You don't have any code. If you want help, try something out first. You're much more likely to get helpful answers. – JasCav May 10 '11 at 03:18
  • I just created a simple function to do this with Joda-Time's `LocalDate` and ISO Day-of-Week: https://stackoverflow.com/a/53422758/501113 – chaotic3quilibrium Feb 20 '23 at 15:44

7 Answers7

7

I won't give an answer (try it yourself first!), but, maybe these tips can help you out.

  1. You first need to figure out the current day of the week you are on. You may want to take a look at Java's Calendar class to get an idea of how to do that.
  2. Once you get the date you are on, think about the modulus operator and how you can use that to move backwards to pick up the previous day that you are looking for from the day you are currently at. (Remember, a week is 7 days and each day of the week takes up a "slot" in those 7 days.)
  3. Once you have the number of days in between, you'll want to subtract. Of course, there are classes that can add and subtract days for you in the Java framework...

I hope that helps. Again, I encourage you to always try the problem for yourself, first. You learn far much more that way and be a better developer in the long run for it.

JasCav
  • 34,458
  • 20
  • 113
  • 170
  • +1 for being patient, informative and encouraging. – ditkin May 10 '11 at 03:34
  • thanks Jas...i tried and got the sollution...:) – MohammedYakub M. May 10 '11 at 10:06
  • 1
    @yakub_moriss - Excellent! Glad to hear it. What I did in this question, try in the future - write out the process first before ever writing any lines of code. That tends to help clear up the problem space a bit. Also, if my answer helped you, I would appreciate the accept. :-) Thanks! – JasCav May 10 '11 at 14:05
3

Here is a brute force idea. Check if current date is friday. If not, subtract 1 day from today. Check if new date is friday. If not, subtract 1 day from new date..... so on.. you get the idea.

Kal
  • 24,724
  • 7
  • 65
  • 65
3

Try this one:

/**
  * Return last day of week before specified date.
  * @param date - reference date.
  * @param day - DoW field from Calendar class.
  * @return
  */
public static Date getDateOnDay(Date date, int day) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.WEEK_OF_YEAR, -1);
    cal.set(Calendar.DAY_OF_WEEK, day);
    return cal.getTime();
}

Good luck.

Andrei Petrenko
  • 3,922
  • 3
  • 31
  • 53
2

I'm using this:

private Date getDateOnDay(Date date, int day) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.setFirstDayOfWeek(day);
    cal.set(Calendar.DAY_OF_WEEK, day);
    return cal.getTime();
}
jose
  • 31
  • 3
1

To get any latest date based on weekday:

private String getWeekDayDate(String weekday){
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    Calendar start = Calendar.getInstance();
    Date now = new Date();
    start.setTime(now);
    Calendar end = Calendar.getInstance();
    end.add(Calendar.DATE, -7);
    while (start.after(end)) 
    {
        try {
            Date temp  = start.getTime();
            String day = new SimpleDateFormat("EEEE").format(temp);
            if(day.equalsIgnoreCase(weekday))
                return formatter.format(temp);
        }catch(Exception e) {
           e.printStackTrace(); 
        }
        start.add(Calendar.DAY_OF_YEAR, -1);
     }
     return null;
}

To get latest Friday date, give weekday as "Friday"

Roja Vangapalli
  • 259
  • 2
  • 5
1

Get the day of week for the date. Look at Calendar javadoc. Once you have the day of the week you can calculate an offset to apply to the date.

ditkin
  • 6,774
  • 1
  • 35
  • 37
-1

//gets the last four Fridays from today's date if you want pass in a any date //just need to tweak the code, the other method just basically formats the date in dd/MM/YYYY format. function GetLastFourFridays() { today = new Date(); LastFridayDate = new Date();

        LastFridayDate.setDate(LastFridayDate.getDate() - 1);

        while (LastFridayDate.getDay() != 5) {
            LastFridayDate.setDate(LastFridayDate.getDate() - 1);
        }
        var lfd = LastFridayDate
        lfd = convertDate(lfd)

        document.getElementById("first_week_th").innerHTML = lfd

        LastFridayDate.setDate(LastFridayDate.getDate() - 1);
        var friLastWeek = LastFridayDate
        while (friLastWeek.getDay() != 5) {
            friLastWeek.setDate(friLastWeek.getDate() - 1);
        }
        var flw = friLastWeek
        flw = convertDate(flw)
        document.getElementById("second_week_th").innerHTML = flw

        friLastWeek.setDate(friLastWeek.getDate() - 1);
        var friTwoWeeks = friLastWeek
        while (friTwoWeeks.getDay() != 5) {
            friTwoWeeks.setDate(friTwoWeeks.getDate() - 1);
        }
        var ftw = friTwoWeeks
        ftw = convertDate(ftw)
        document.getElementById("third_week_th").innerHTML = ftw


        friTwoWeeks.setDate(friTwoWeeks.getDate() - 1);
        var friThreeWeeks = friTwoWeeks
        while (friThreeWeeks.getDay() != 5) {
            friThreeWeeks.setDate(friThreeWeeks.getDate() - 1);
        }
        var ftww = friThreeWeeks
        ftww = convertDate(ftww)
        document.getElementById("fourth_week_th").innerHTML = ftww
    }

//convets the date 00//00//0000 function convertDate(inputFormat) { function pad(s) { return (s < 10) ? '0' + s : s; } var d = new Date(inputFormat); return [pad(d.getDate()), pad(d.getMonth()+1), d.getFullYear()].join('/');}

Pec1983
  • 346
  • 4
  • 8