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
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Calling code
Output
this should do what you want.
edit: with some more calculate steps, you could have result :) (sorry for confuse your title)
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.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: