受到以下帖子的启发
- Get the first Monday of a month
- Java: How do I get the date of x day in a month ( e.g. Third Monday in February 2012)
我需要一个函数来返回一个月中给定日期的序号位置,例如:
01/01/1970 = 1 because it's the first Thursday in January, 1970
02/01/1970 = 1 because it's the first Friday in January, 1970
19/01/1970 = 3 because it's the third Monday in January, 1970
31/01/1970 = 5 because it's the fifth Saturday in January, 1970
我尝试了什么? - 没什么……我什至不知道从哪里开始; Java 8 日期/时间 API 对我来说很新。
理想情况下,我想要一个具有此签名的函数:
public int getOrdinalPosition(TemporalAccessor temporal) {
...
}
最佳答案
嗯,以 19/01/1970 为例。从中减去 7 天,它仍然是 1 月。再次减去 7 天,然后……仍然是 1 月。第 4 次减去 7 天,然后......哦,嘿,它不再是 1 月了。第 4 次删除 7 天时,它不再是正确的月份。
这就是你所需要的。
相关方法:for 循环、计数器、minusDays(7)
方法和 getMonth()
。
关于Java 8 Date API - 获取月份中日期的序号位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54058102/