如何获取一个月中的第几个工作日
嗨,我想用java制作一个程序,其中days,weekNo是参数..比如该月的第一个星期五或该月的第二个星期一..它返回日期
hi i want to make a program in java where days,weekNo is parameter ..Like First Friday of the month or second Monday of the month ..and it returns the date
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个实用程序方法,使用 来自 Apache Commons / Lang 的
DateUtils
:测试代码:
输出:
这是相同代码的 JodaTime 版本(我'我以前从未使用过 JodaTime,所以可能有一种更简单的方法来做到这一点):
测试代码:
输出:
Here's a utility method that does that, using
DateUtils
from Apache Commons / Lang:Test code:
Output:
And here's a JodaTime version of the same code (I've never used JodaTime before, so there's probably a simpler way to do it):
Test Code:
Output:
调用代码
输出
Calling code
Output
tl;dr
使用 java.time
其他答案现已过时。麻烦的旧日期时间类(
Date
、Calendar
等)现在是 遗留,被 java.time 类取代。LocalDate
LocalDate
类表示仅日期值,没有时间和时区。时区对于确定日期至关重要。对于任何特定时刻,全球各地的日期都会因地区而异。例如,在法国巴黎午夜过后几分钟,又是新的一天“昨天”在魁北克蒙特利尔。
以
大陆/地区
格式指定正确的时区名称 ,例如America/Montreal
、非洲/卡萨布兰卡
,或太平洋/奥克兰
。切勿使用 3-4 个字母的缩写,例如EST
或IST
,因为它们不是真正的时区,不是标准化的,甚至不是唯一的( !)。TemporalAdjuster
< code>TemporalAdjuster 接口提供用于操作日期时间值。 java.time 类使用不可变对象,因此结果始终是一个全新的对象,其值基于原始对象。
TemporalAdjusters
< /a> 类(注意复数名称)提供了几种方便的实现。其中包括获取当月内星期几的方法:firstInMonth()
、lastInMonth()
和dayOfWeekInMonth()
。所有这些都采用 DayOfWeek 枚举对象作为参数。…以及…
…以及…
关于 java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如
java.util.Date
,日历
, & ;SimpleDateFormat
。Joda-Time 项目,现已在 维护模式,建议迁移到 java.time 类。
要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310。
从哪里获取 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如
间隔
,YearWeek
,<代码>YearQuarter,以及更多 。tl;dr
Using java.time
The other Answers are now outdated. The troublesome old date-time classes (
Date
,Calendar
, etc.) are now legacy, supplanted by the java.time classes.LocalDate
The
LocalDate
class represents a date-only value without time-of-day and without time zone.A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
Specify a proper time zone name in the format of
continent/region
, such asAmerica/Montreal
,Africa/Casablanca
, orPacific/Auckland
. Never use the 3-4 letter abbreviation such asEST
orIST
as they are not true time zones, not standardized, and not even unique(!).TemporalAdjuster
The
TemporalAdjuster
interface provides for manipulating date-time values. The java.time classes use immutable objects, so the result is always a fresh new object with values based on the original.The
TemporalAdjusters
class (note plural name) provides several handy implementations. Amongst those are ones to get ordinal day-of-week within the month:firstInMonth()
,lastInMonth()
, anddayOfWeekInMonth()
. All of these take an argument of aDayOfWeek
enum object.…and…
…and…
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.这应该做你想要的。
编辑:
通过更多的计算步骤,您可能会得到结果:)(抱歉混淆了您的标题)
this should do what you want.
edit:
with some more calculate steps, you could have result :) (sorry for confuse your title)