如何从指定日期获取上周五的日期?

发布于 2024-11-06 06:07:08 字数 126 浏览 1 评论 0原文

如何找出上一个(上一个)“星期五”或指定日期的任何其他日期的日期?

public getDateOnDay(Date date, String dayName) {
    // ?
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

空心↖ 2024-11-13 06:07:08

我不会给出答案(先自己尝试一下!),但是,也许这些提示可以帮助您。

  1. 您首先需要弄清楚您所在的星期几。您可能想看看 Java 的 日历类来了解如何做到这一点。
  2. 获得当前日期后,请考虑模数运算符以及如何使用它向后移动以从当前所在的日期开始查找您要查找的前一天。 (请记住,一周有 7 天,一周中的每一天都在这 7 天中占据一个“时段”。)
  3. 一旦获得了中间的天数,您就需要减去。当然,Java框架中有一些可以为您加减天数的类...

希望对您有所帮助。我再次鼓励您首先亲自尝试一下这个问题。通过这种方式,你可以学到更多东西,从长远来看,你可以成为一名更好的开发人员。

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.

后来的我们 2024-11-13 06:07:08

这是一个蛮力的想法。检查当前日期是否为星期五。如果不是,则从今天减去 1 天。检查新日期是否为星期五。如果不是,则从新日期中减去 1 天......依此类推......你明白了。

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.

清风夜微凉 2024-11-13 06:07:08

试试这个:

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

祝你好运。

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.

娇纵 2024-11-13 06:07:08

我正在使用这个:

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

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();
}
你如我软肋 2024-11-13 06:07:08

获取日期是星期几。查看日历 javadoc。一旦获得了星期几,您就可以计算适用于该日期的偏移量。

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.

柠北森屋 2024-11-13 06:07:08

要获取基于工作日的任何最新日期:

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 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"

罗罗贝儿 2024-11-13 06:07:08

//如果您想传入任何日期,则获取从今天开始的最后四个星期五
//只需要调整代码,另一种方法只是基本上以 dd/MM/YYYY 格式格式化日期。
函数 GetLastFourFridays() {
今天=新日期();
最后星期五日期 = 新日期();

        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
    }

//转换日期00//00//0000
函数转换日期(输入格式){
函数 pad(s) { return (s < 10) ? '0' + s : s; }
var d = new Date(inputFormat);
返回 [pad(d.getDate()), pad(d.getMonth()+1), d.getFullYear()].join('/');}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文