Is there easiest way to find any day is in the current week? (this function returns true or false, related to given day is in current week or not).
相关问题
- 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
You definitely want to use the Calendar class: http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html
Here's one way to do it:
Simple Kotlin solution:
This answer is for Android. Put your selected date Calendar object in below method to check your selected date is current week date or not.
Android already provides the
java.util.Calendar
class for any date and time related query.tl;dr
What's a week? Sunday-Saturday? Monday-Sunday? An ISO 8601 week?
For a standard ISO 8601 week…
java.time
The modern approach uses the java.time classes that supplant the troublesome old date-time classes such as
Date
&Calendar
.In addition, the ThreeTen-Extra project provides a handy
YearWeek
class for our purposes here, assuming a standard ISO 8601 week is what you had in mind by the word "week". The documentation for that class summarized the definition of a standard week: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(!).The
LocalDate
class represents a date-only value without time-of-day and without time zone.To see if a particular date is contained within the target
YearWeek
object’s dates, get theYearWeek
of that date and compare by callingequals
.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.Joda-Time
UPDATE: The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. Leaving this section intact for history.
Checking for ISO week is easy in Joda-Time 2.3.
When run…
Use the
Calendar
class to get the YEAR and WEEK_OF_YEAR fields and see if both are the same.Note that the result will be different depending on the locale, since different cultures have different opinions about which day is the first day of the week.