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).
5 Answers
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:
public static boolean isDateInCurrentWeek(Date date) {
Calendar currentCalendar = Calendar.getInstance();
int week = currentCalendar.get(Calendar.WEEK_OF_YEAR);
int year = currentCalendar.get(Calendar.YEAR);
Calendar targetCalendar = Calendar.getInstance();
targetCalendar.setTime(date);
int targetWeek = targetCalendar.get(Calendar.WEEK_OF_YEAR);
int targetYear = targetCalendar.get(Calendar.YEAR);
return week == targetWeek && year == targetYear;
}

- 6,090
- 21
- 19
-
thanks for fast answer, however, you have to use "Calendar currentCalendar = Calendar.getInstance();" instead of "Calendar currentCalendar = new Calendar();" – Ikrom Apr 25 '12 at 10:44
-
2Now outdated. The troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 09 '17 at 21:02
-
Thanks for the answer! – Ketan Mehta Nov 11 '17 at 07:30
-
1Are Monday, December 31, 2018 and Wednesday, Januar 2, 2019 in the same week? To me they are. Since they are in different years, your code will say they are not. – Ole V.V. Jun 30 '18 at 09:50
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.

- 342,105
- 78
- 482
- 720
tl;dr
What's a week? Sunday-Saturday? Monday-Sunday? An ISO 8601 week?
For a standard ISO 8601 week…
YearWeek.now( ZoneId.of( "America/Montreal" ) )
.equals(
YearWeek.from(
LocalDate.of( 2017 , Month.JANUARY , 23 )
)
)
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:
ISO-8601 defines the week as always starting with Monday. The first week is the week which contains the first Thursday of the calendar year. As such, the week-based-year used in this class does not align with the calendar year.
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 as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" );
YearWeek ywNow = YearWeek.now( z ) ; // Get the current week for this specific time zone.
The LocalDate
class represents a date-only value without time-of-day and without time zone.
LocalDate ld = LocalDate.of( 2017 , Month.JANUARY , 23 ) ; // Some date.
To see if a particular date is contained within the target YearWeek
object’s dates, get the YearWeek
of that date and compare by calling equals
.
YearWeek ywThen = YearWeek.from( ld ) ; // Determine year-week of some date.
Boolean isSameWeek = ywThen.equals( ywNow ) ;
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
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 brought some minor features and fixes.
- Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android (26+) bundle implementations of the java.time classes.
- For earlier Android (<26), a process known as API desugaring brings a subset of the java.time functionality not originally built into Android.
- If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….
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.
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
DateTimeZone parisTimeZone = DateTimeZone.forID( "Europe/Paris" );
DateTime now = new DateTime( parisTimeZone );
DateTime yesterday = now.minusDays( 1 );
DateTime inThree = now.plusDays( 3 );
DateTime inFourteen = now.plusDays( 14 );
System.out.println( "Now: " + now + " is ISO week: " + now.weekOfWeekyear().get() );
System.out.println( "Same ISO week as yesterday ( " + yesterday + " week "+ yesterday.weekOfWeekyear().get() +" ): " + ( now.weekOfWeekyear().get() == yesterday.weekOfWeekyear().get() ) );
System.out.println( "Same ISO week as inThree ( " + inThree + " week "+ inThree.weekOfWeekyear().get() +" ): " + ( now.weekOfWeekyear().get() == inThree.weekOfWeekyear().get() ) );
System.out.println( "Same ISO week as inFourteen ( " + inFourteen + " week "+ inFourteen.weekOfWeekyear().get() +" ): " + ( now.weekOfWeekyear().get() == inFourteen.weekOfWeekyear().get() ) );
When run…
Now: 2013-12-12T06:32:49.020+01:00 is ISO week: 50
Same ISO week as yesterday ( 2013-12-11T06:32:49.020+01:00 week 50 ): true
Same ISO week as inThree ( 2013-12-15T06:32:49.020+01:00 week 50 ): true
Same ISO week as inFourteen ( 2013-12-26T06:32:49.020+01:00 week 52 ): false

- 303,325
- 100
- 852
- 1,154
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.
private boolean isCurrentWeekDateSelect(Calendar yourSelectedDate) {
Date ddd = yourSelectedDate.getTime();
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
Date monday = c.getTime();
Date nextMonday= new Date(monday.getTime()+7*24*60*60*1000);
return ddd.after(monday) && ddd.before(nextMonday);
}
Android already provides the java.util.Calendar
class for any date and time related query.

- 81,772
- 15
- 137
- 161

- 3,328
- 28
- 31
-
2The troublesome `Calendar` is now legacy, supplanted years ago by the *java.tine*. Suggesting these classes in 2018 is poor advice. – Basil Bourque Jun 30 '18 at 06:01
-
above answer is for android... and android is already provide **java.util.Calendar** class for any date and time related query. – Pankaj Talaviya Jun 30 '18 at 06:23
-
1The Question is not for Android. And even if it were, Android 26 and later carries an implantation of *java.time*. For earlier Android, the *ThreeTenABP* project adapts the *ThreeTen-Backport* project that has much of the *java.time* functionality back-ported to Java 6 & 7. See [my Answer](https://stackoverflow.com/a/20535788/642706) for links. No reason to ever touch those awful old date-time classes again. – Basil Bourque Jun 30 '18 at 06:30
-
Thanks for wanting to contribute an answer for Android. Please state so in the answer itself (not just in a comment) so readers are not mislead. Also I agree with @BasilBourque that suggesting this complicated solution using `Calendar` and some homegrown math without mentioning the much better alternative (also available for low Android API levels if you accept an external dependency) is not good advice. – Ole V.V. Jun 30 '18 at 09:55
Simple Kotlin solution:
fun isDateInCurrentWeek(date: DateTime): Boolean {
val cal = Calendar.getInstance()
cal.add(Calendar.DAY_OF_WEEK, 0)
var dt= DateTime(cal.timeInMillis)
return date in dt..dt.plusDays(6)
}

- 1,255
- 11
- 13
-
It’s like you are answering a different question? Maybe one like [Java: how do I check if a Date is within a certain range?](https://stackoverflow.com/questions/494180/java-how-do-i-check-if-a-date-is-within-a-certain-range) Also were `sDate` and `startDateTime` supposed to be the same? And is `DateTime` from Joda-time or something built into Kotlin? – Ole V.V. Sep 04 '19 at 08:03
-