开发者

How to get date of last Friday from specified date? [duplicate]

This question already has answers here: Time: How to get the next friday? (9 answers) Closed 6 years ago. 开发者_StackOverflow

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) {
    // ?
}


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.


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.


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.


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();
}


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.


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"


//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('/');}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜