How to get date of last Friday from specified date?(如何从指定日期获取上周五的日期?)
问题描述
我怎样才能找到上一个(前一个)星期五"的日期或从指定日期开始的任何其他日期?
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.
- 您首先需要确定您所在的星期几.您可能想看看 Java 的 日历类来了解如何做到这一点.
- 一旦您获得了您所在的日期,请考虑模数运算符以及如何使用它向后移动以从您当前所在的那一天开始获取您正在寻找的前一天.(请记住,一周有 7 天,一周中的每一天在这 7 天中占据一个空档".)
- 一旦确定了两者之间的天数,您就会想减去.当然,Java框架中也有可以为你加减天数的类……
- 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.
- 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.)
- 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.
这篇关于如何从指定日期获取上周五的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从指定日期获取上周五的日期?


基础教程推荐
- 不推荐使用 Api 注释的描述 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 多个组件的复杂布局 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 从 python 访问 JVM 2022-01-01