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.
这篇关于如何从指定日期获取上周五的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从指定日期获取上周五的日期?


- 未找到/usr/local/lib 中的库 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 转换 ldap 日期 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 获取数字的最后一位 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01