java从ZoneDateTime获取月份的哪一天
我想从给定的ZonedDateTime
中找出一个月的哪一天。
我知道今天是星期四,想知道一个月的哪个星期四?
例如:
2021-06-10T13:30:30+03:00等于2。星期四
2021-06-17T13:30:30+03:00是3。星期四
2021-06-24T13:30:30+03:00是上周四
等等
private static int getWhicDayOfMonth(ZonedDateTime date) { }
做这件事最简单的方法是什么
# 1 楼答案
基本上与@ArvindKumarAvinash提供的答案中的方法相同,但循环有点不同:
您可以这样使用它:
并收到回复
在你的控制台里
# 2 楼答案
在您的示例中:
或者简化为
date.getDayOfMonth() + 6) / 7
——但我倾向于发现这更容易让人困惑# 3 楼答案
从月内第一次出现的一周中的某一天开始,以7天的步长值循环该月的所有天,并不断递增计数器,直到匹配给定的日期
演示:
输出:
ONLINE DEMO
从Trail: Date Time了解有关现代日期时间API的更多信息